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 uint32_t *vm_page_dump;
61 int vm_page_dump_size;
63 static struct kerneldumpheader kdh;
65 /* Handle chunked writes. */
68 static uint64_t counter, progress;
70 CTASSERT(sizeof(*vm_page_dump) == 4);
73 is_dumpable(vm_paddr_t pa)
77 for (i = 0; dump_avail[i] != 0 || dump_avail[i + 1] != 0; i += 2) {
78 if (pa >= dump_avail[i] && pa < dump_avail[i + 1])
84 #define PG2MB(pgs) (((pgs) + (1 << 8) - 1) >> 8)
87 blk_flush(struct dumperinfo *di)
94 error = dump_append(di, dump_va, 0, fragsz);
100 blk_write(struct dumperinfo *di, char *ptr, vm_paddr_t pa, size_t sz)
106 maxdumpsz = min(di->maxiosize, MAXDUMPPGS * PAGE_SIZE);
107 if (maxdumpsz == 0) /* seatbelt */
108 maxdumpsz = PAGE_SIZE;
110 if (ptr != NULL && pa != 0) {
111 printf("cant have both va and pa!\n");
115 if ((sz % PAGE_SIZE) != 0) {
116 printf("size not page aligned\n");
119 if ((pa & PAGE_MASK) != 0) {
120 printf("address not page aligned\n");
125 /* Flush any pre-existing pa pages before a virtual dump. */
126 error = blk_flush(di);
131 len = maxdumpsz - fragsz;
137 printf(" %lld", PG2MB(progress >> PAGE_SHIFT));
138 counter &= (1<<22) - 1;
142 wdog_kern_pat(WD_LASTVAL);
145 error = dump_append(di, ptr, 0, len);
151 for (i = 0; i < len; i += PAGE_SIZE)
152 dump_va = pmap_kenter_temporary(pa + i,
153 (i + fragsz) >> PAGE_SHIFT);
157 if (fragsz == maxdumpsz) {
158 error = blk_flush(di);
164 /* Check for user abort. */
169 printf(" (CTRL-C to abort) ");
175 /* A buffer for general use. Its size must be one page at least. */
176 static char dumpbuf[PAGE_SIZE];
177 CTASSERT(sizeof(dumpbuf) % sizeof(pt2_entry_t) == 0);
180 minidumpsys(struct dumperinfo *di)
182 struct minidumphdr mdhdr;
186 uint32_t pa, prev_pa = 0, count = 0;
192 * Flush caches. Note that in the SMP case this operates only on the
193 * current CPU's L1 cache. Before we reach this point, code in either
194 * the system shutdown or kernel debugger has called stop_cpus() to stop
195 * all cores other than this one. Part of the ARM handling of
196 * stop_cpus() is to call wbinv_all() on that core's local L1 cache. So
197 * by time we get to here, all that remains is to flush the L1 for the
198 * current CPU, then the L2.
200 dcache_wbinv_poc_all();
203 /* Walk page table pages, set bits in vm_page_dump */
205 for (va = KERNBASE; va < kernel_vm_end; va += PAGE_SIZE) {
206 pa = pmap_dump_kextract(va, NULL);
207 if (pa != 0 && is_dumpable(pa))
209 ptesize += sizeof(pt2_entry_t);
212 /* Calculate dump size. */
214 dumpsize += round_page(msgbufp->msg_size);
215 dumpsize += round_page(vm_page_dump_size);
216 for (i = 0; i < vm_page_dump_size / sizeof(*vm_page_dump); i++) {
217 bits = vm_page_dump[i];
220 pa = (((uint64_t)i * sizeof(*vm_page_dump) * NBBY) +
222 /* Clear out undumpable pages now if needed */
224 dumpsize += PAGE_SIZE;
227 bits &= ~(1ul << bit);
230 dumpsize += PAGE_SIZE;
234 /* Initialize mdhdr */
235 bzero(&mdhdr, sizeof(mdhdr));
236 strcpy(mdhdr.magic, MINIDUMP_MAGIC);
237 mdhdr.version = MINIDUMP_VERSION;
238 mdhdr.msgbufsize = msgbufp->msg_size;
239 mdhdr.bitmapsize = vm_page_dump_size;
240 mdhdr.ptesize = ptesize;
241 mdhdr.kernbase = KERNBASE;
242 mdhdr.arch = __ARM_ARCH;
244 mdhdr.mmuformat = MINIDUMP_MMU_FORMAT_V6;
246 mdhdr.mmuformat = MINIDUMP_MMU_FORMAT_V4;
248 dump_init_header(di, &kdh, KERNELDUMPMAGIC, KERNELDUMP_ARM_VERSION,
251 error = dump_start(di, &kdh);
255 printf("Physical memory: %u MB\n", ptoa((uintmax_t)physmem) / 1048576);
256 printf("Dumping %llu MB:", (long long)dumpsize >> 20);
259 bzero(dumpbuf, sizeof(dumpbuf));
260 bcopy(&mdhdr, dumpbuf, sizeof(mdhdr));
261 error = blk_write(di, dumpbuf, 0, PAGE_SIZE);
265 /* Dump msgbuf up front */
266 error = blk_write(di, (char *)msgbufp->msg_ptr, 0,
267 round_page(msgbufp->msg_size));
272 error = blk_write(di, (char *)vm_page_dump, 0,
273 round_page(vm_page_dump_size));
277 /* Dump kernel page table pages */
279 for (va = KERNBASE; va < kernel_vm_end; va += PAGE_SIZE) {
280 pmap_dump_kextract(va, (pt2_entry_t *)addr);
281 addr += sizeof(pt2_entry_t);
282 if (addr == dumpbuf + sizeof(dumpbuf)) {
283 error = blk_write(di, dumpbuf, 0, sizeof(dumpbuf));
289 if (addr != dumpbuf) {
290 error = blk_write(di, dumpbuf, 0, addr - dumpbuf);
295 /* Dump memory chunks */
296 for (i = 0; i < vm_page_dump_size / sizeof(*vm_page_dump); i++) {
297 bits = vm_page_dump[i];
300 pa = (((uint64_t)i * sizeof(*vm_page_dump) * NBBY) +
306 if (pa == (prev_pa + count * PAGE_SIZE))
309 error = blk_write(di, NULL, prev_pa,
317 bits &= ~(1ul << bit);
321 error = blk_write(di, NULL, prev_pa, count * PAGE_SIZE);
328 error = blk_flush(di);
332 error = dump_finish(di, &kdh);
336 printf("\nDump complete\n");
343 if (error == ECANCELED)
344 printf("\nDump aborted\n");
345 else if (error == E2BIG || error == ENOSPC) {
346 printf("\nDump failed. Partition too small (about %lluMB were "
347 "needed this time).\n", (long long)dumpsize >> 20);
349 printf("\n** DUMP FAILED (ERROR %d) **\n", error);
354 dump_add_page(vm_paddr_t pa)
359 idx = pa >> 5; /* 2^5 = 32 */
361 atomic_set_int(&vm_page_dump[idx], 1ul << bit);
365 dump_drop_page(vm_paddr_t pa)
370 idx = pa >> 5; /* 2^5 = 32 */
372 atomic_clear_int(&vm_page_dump[idx], 1ul << bit);