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>
51 #include <vm/vm_dumpset.h>
53 #include <machine/atomic.h>
54 #include <machine/cpu.h>
55 #include <machine/elf.h>
56 #include <machine/md_var.h>
57 #include <machine/minidump.h>
59 CTASSERT(sizeof(struct kerneldumpheader) == 512);
61 static struct kerneldumpheader kdh;
63 /* Handle chunked writes. */
66 static uint64_t counter, progress;
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] __aligned(sizeof(uint64_t));
173 CTASSERT(sizeof(dumpbuf) % sizeof(pt2_entry_t) == 0);
176 minidumpsys(struct dumperinfo *di)
178 struct minidumphdr mdhdr;
179 uint64_t dumpsize, *dump_avail_buf;
181 uint32_t pa, prev_pa = 0, count = 0;
187 * Flush caches. Note that in the SMP case this operates only on the
188 * current CPU's L1 cache. Before we reach this point, code in either
189 * the system shutdown or kernel debugger has called stop_cpus() to stop
190 * all cores other than this one. Part of the ARM handling of
191 * stop_cpus() is to call wbinv_all() on that core's local L1 cache. So
192 * by time we get to here, all that remains is to flush the L1 for the
193 * current CPU, then the L2.
195 dcache_wbinv_poc_all();
198 /* Walk page table pages, set bits in vm_page_dump */
200 for (va = KERNBASE; va < kernel_vm_end; va += PAGE_SIZE) {
201 pa = pmap_dump_kextract(va, NULL);
202 if (pa != 0 && is_dumpable(pa))
204 ptesize += sizeof(pt2_entry_t);
207 /* Calculate dump size. */
209 dumpsize += round_page(msgbufp->msg_size);
210 dumpsize += round_page(nitems(dump_avail) * sizeof(uint64_t));
211 dumpsize += round_page(BITSET_SIZE(vm_page_dump_pages));
212 VM_PAGE_DUMP_FOREACH(pa) {
213 /* Clear out undumpable pages now if needed */
215 dumpsize += PAGE_SIZE;
219 dumpsize += PAGE_SIZE;
223 /* Initialize mdhdr */
224 bzero(&mdhdr, sizeof(mdhdr));
225 strcpy(mdhdr.magic, MINIDUMP_MAGIC);
226 mdhdr.version = MINIDUMP_VERSION;
227 mdhdr.msgbufsize = msgbufp->msg_size;
228 mdhdr.bitmapsize = round_page(BITSET_SIZE(vm_page_dump_pages));
229 mdhdr.ptesize = ptesize;
230 mdhdr.kernbase = KERNBASE;
231 mdhdr.arch = __ARM_ARCH;
232 mdhdr.mmuformat = MINIDUMP_MMU_FORMAT_V6;
233 mdhdr.dumpavailsize = round_page(nitems(dump_avail) * sizeof(uint64_t));
235 dump_init_header(di, &kdh, KERNELDUMPMAGIC, KERNELDUMP_ARM_VERSION,
238 error = dump_start(di, &kdh);
242 printf("Physical memory: %u MB\n", ptoa((uintmax_t)physmem) / 1048576);
243 printf("Dumping %llu MB:", (long long)dumpsize >> 20);
246 bzero(dumpbuf, sizeof(dumpbuf));
247 bcopy(&mdhdr, dumpbuf, sizeof(mdhdr));
248 error = blk_write(di, dumpbuf, 0, PAGE_SIZE);
252 /* Dump msgbuf up front */
253 error = blk_write(di, (char *)msgbufp->msg_ptr, 0,
254 round_page(msgbufp->msg_size));
258 /* Dump dump_avail. Make a copy using 64-bit physical addresses. */
259 _Static_assert(nitems(dump_avail) * sizeof(uint64_t) <= sizeof(dumpbuf),
260 "Large dump_avail not handled");
261 bzero(dumpbuf, sizeof(dumpbuf));
262 dump_avail_buf = (uint64_t *)dumpbuf;
263 for (i = 0; dump_avail[i] != 0 || dump_avail[i + 1] != 0; i += 2) {
264 dump_avail_buf[i] = dump_avail[i];
265 dump_avail_buf[i + 1] = dump_avail[i + 1];
267 error = blk_write(di, dumpbuf, 0, PAGE_SIZE);
272 error = blk_write(di, (char *)vm_page_dump, 0,
273 round_page(BITSET_SIZE(vm_page_dump_pages)));
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 VM_PAGE_DUMP_FOREACH(pa) {
301 if (pa == (prev_pa + count * PAGE_SIZE))
304 error = blk_write(di, NULL, prev_pa,
314 error = blk_write(di, NULL, prev_pa, count * PAGE_SIZE);
321 error = blk_flush(di);
325 error = dump_finish(di, &kdh);
329 printf("\nDump complete\n");
336 if (error == ECANCELED)
337 printf("\nDump aborted\n");
338 else if (error == E2BIG || error == ENOSPC) {
339 printf("\nDump failed. Partition too small (about %lluMB were "
340 "needed this time).\n", (long long)dumpsize >> 20);
342 printf("\n** DUMP FAILED (ERROR %d) **\n", error);