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>
44 #include <sys/watchdog.h>
46 #include <vm/vm_param.h>
47 #include <vm/vm_page.h>
48 #include <vm/vm_phys.h>
49 #include <vm/vm_dumpset.h>
51 #include <machine/atomic.h>
52 #include <machine/cpu.h>
53 #include <machine/elf.h>
54 #include <machine/md_var.h>
55 #include <machine/minidump.h>
57 CTASSERT(sizeof(struct kerneldumpheader) == 512);
59 static struct kerneldumpheader kdh;
61 /* Handle chunked writes. */
66 blk_flush(struct dumperinfo *di)
73 error = dump_append(di, dump_va, fragsz);
79 blk_write(struct dumperinfo *di, char *ptr, vm_paddr_t pa, size_t sz)
85 maxdumpsz = min(di->maxiosize, MAXDUMPPGS * PAGE_SIZE);
86 if (maxdumpsz == 0) /* seatbelt */
87 maxdumpsz = PAGE_SIZE;
89 if (ptr != NULL && pa != 0) {
90 printf("cant have both va and pa!\n");
94 if ((sz % PAGE_SIZE) != 0) {
95 printf("size not page aligned\n");
98 if ((pa & PAGE_MASK) != 0) {
99 printf("address not page aligned\n");
104 /* Flush any pre-existing pa pages before a virtual dump. */
105 error = blk_flush(di);
110 len = maxdumpsz - fragsz;
114 dumpsys_pb_progress(len);
115 wdog_kern_pat(WD_LASTVAL);
118 error = dump_append(di, ptr, len);
124 for (i = 0; i < len; i += PAGE_SIZE)
125 dump_va = pmap_kenter_temporary(pa + i,
126 (i + fragsz) >> PAGE_SHIFT);
130 if (fragsz == maxdumpsz) {
131 error = blk_flush(di);
137 /* Check for user abort. */
142 printf(" (CTRL-C to abort) ");
148 /* A buffer for general use. Its size must be one page at least. */
149 static char dumpbuf[PAGE_SIZE] __aligned(sizeof(uint64_t));
150 CTASSERT(sizeof(dumpbuf) % sizeof(pt2_entry_t) == 0);
153 cpu_minidumpsys(struct dumperinfo *di, const struct minidumpstate *state)
155 struct minidumphdr mdhdr;
157 uint64_t dumpsize, *dump_avail_buf;
159 uint32_t pa, prev_pa = 0, count = 0;
160 vm_offset_t va, kva_end;
165 * Flush caches. Note that in the SMP case this operates only on the
166 * current CPU's L1 cache. Before we reach this point, code in either
167 * the system shutdown or kernel debugger has called stop_cpus() to stop
168 * all cores other than this one. Part of the ARM handling of
169 * stop_cpus() is to call wbinv_all() on that core's local L1 cache. So
170 * by time we get to here, all that remains is to flush the L1 for the
171 * current CPU, then the L2.
173 dcache_wbinv_poc_all();
175 /* Snapshot the KVA upper bound in case it grows. */
176 kva_end = kernel_vm_end;
179 * Walk the kernel page table pages, setting the active entries in the
183 for (va = KERNBASE; va < kva_end; va += PAGE_SIZE) {
184 pa = pmap_dump_kextract(va, NULL);
185 if (pa != 0 && vm_phys_is_dumpable(pa))
186 vm_page_dump_add(state->dump_bitset, pa);
187 ptesize += sizeof(pt2_entry_t);
190 /* Calculate dump size. */
191 mbp = state->msgbufp;
193 dumpsize += round_page(mbp->msg_size);
194 dumpsize += round_page(nitems(dump_avail) * sizeof(uint64_t));
195 dumpsize += round_page(BITSET_SIZE(vm_page_dump_pages));
196 VM_PAGE_DUMP_FOREACH(state->dump_bitset, pa) {
197 /* Clear out undumpable pages now if needed */
198 if (vm_phys_is_dumpable(pa))
199 dumpsize += PAGE_SIZE;
201 vm_page_dump_drop(state->dump_bitset, pa);
203 dumpsize += PAGE_SIZE;
205 dumpsys_pb_init(dumpsize);
207 /* Initialize mdhdr */
208 bzero(&mdhdr, sizeof(mdhdr));
209 strcpy(mdhdr.magic, MINIDUMP_MAGIC);
210 mdhdr.version = MINIDUMP_VERSION;
211 mdhdr.msgbufsize = mbp->msg_size;
212 mdhdr.bitmapsize = round_page(BITSET_SIZE(vm_page_dump_pages));
213 mdhdr.ptesize = ptesize;
214 mdhdr.kernbase = KERNBASE;
215 mdhdr.arch = __ARM_ARCH;
216 mdhdr.mmuformat = MINIDUMP_MMU_FORMAT_V6;
217 mdhdr.dumpavailsize = round_page(nitems(dump_avail) * sizeof(uint64_t));
219 dump_init_header(di, &kdh, KERNELDUMPMAGIC, KERNELDUMP_ARM_VERSION,
222 error = dump_start(di, &kdh);
226 printf("Physical memory: %u MB\n", ptoa((uintmax_t)physmem) / 1048576);
227 printf("Dumping %llu MB:", (long long)dumpsize >> 20);
230 bzero(dumpbuf, sizeof(dumpbuf));
231 bcopy(&mdhdr, dumpbuf, sizeof(mdhdr));
232 error = blk_write(di, dumpbuf, 0, PAGE_SIZE);
236 /* Dump msgbuf up front */
237 error = blk_write(di, mbp->msg_ptr, 0, round_page(mbp->msg_size));
241 /* Dump dump_avail. Make a copy using 64-bit physical addresses. */
242 _Static_assert(nitems(dump_avail) * sizeof(uint64_t) <= sizeof(dumpbuf),
243 "Large dump_avail not handled");
244 bzero(dumpbuf, sizeof(dumpbuf));
245 dump_avail_buf = (uint64_t *)dumpbuf;
246 for (i = 0; dump_avail[i] != 0 || dump_avail[i + 1] != 0; i += 2) {
247 dump_avail_buf[i] = dump_avail[i];
248 dump_avail_buf[i + 1] = dump_avail[i + 1];
250 error = blk_write(di, dumpbuf, 0, PAGE_SIZE);
255 error = blk_write(di, (char *)state->dump_bitset, 0,
256 round_page(BITSET_SIZE(vm_page_dump_pages)));
260 /* Dump kernel page table pages */
262 for (va = KERNBASE; va < kva_end; va += PAGE_SIZE) {
263 pmap_dump_kextract(va, (pt2_entry_t *)addr);
264 addr += sizeof(pt2_entry_t);
265 if (addr == dumpbuf + sizeof(dumpbuf)) {
266 error = blk_write(di, dumpbuf, 0, sizeof(dumpbuf));
272 if (addr != dumpbuf) {
273 error = blk_write(di, dumpbuf, 0, addr - dumpbuf);
278 /* Dump memory chunks */
279 VM_PAGE_DUMP_FOREACH(state->dump_bitset, pa) {
284 if (pa == (prev_pa + count * PAGE_SIZE))
287 error = blk_write(di, NULL, prev_pa,
297 error = blk_write(di, NULL, prev_pa, count * PAGE_SIZE);
304 error = blk_flush(di);
308 error = dump_finish(di, &kdh);
312 printf("\nDump complete\n");
319 if (error == ECANCELED)
320 printf("\nDump aborted\n");
321 else if (error == E2BIG || error == ENOSPC) {
322 printf("\nDump failed. Partition too small (about %lluMB were "
323 "needed this time).\n", (long long)dumpsize >> 20);
325 printf("\n** DUMP FAILED (ERROR %d) **\n", error);