2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2010 Oleksandr Tymoshenko <gonzo@freebsd.org>
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/arm/arm/minidump_machdep.c v214223
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
36 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/kerneldump.h>
41 #include <sys/msgbuf.h>
42 #include <sys/watchdog.h>
43 #include <sys/vmmeter.h>
46 #include <vm/vm_page.h>
47 #include <vm/vm_phys.h>
48 #include <machine/atomic.h>
49 #include <machine/elf.h>
50 #include <machine/md_var.h>
51 #include <machine/vmparam.h>
52 #include <machine/minidump.h>
53 #include <machine/cache.h>
55 CTASSERT(sizeof(struct kerneldumpheader) == 512);
57 static struct kerneldumpheader kdh;
59 /* Handle chunked writes. */
60 static uint64_t counter, progress, dumpsize;
61 /* Just auxiliary bufffer */
62 static char tmpbuffer[PAGE_SIZE];
64 extern pd_entry_t *kernel_segmap;
67 is_dumpable(vm_paddr_t pa)
72 if ((m = vm_phys_paddr_to_vm_page(pa)) != NULL)
73 return ((m->flags & PG_NODUMP) == 0);
74 for (i = 0; dump_avail[i] != 0 || dump_avail[i + 1] != 0; i += 2) {
75 if (pa >= dump_avail[i] && pa < dump_avail[i + 1])
85 } progress_track[10] = {
99 report_progress(uint64_t progress, uint64_t dumpsize)
103 sofar = 100 - ((progress * 100) / dumpsize);
104 for (i = 0; i < nitems(progress_track); i++) {
105 if (sofar < progress_track[i].min_per ||
106 sofar > progress_track[i].max_per)
108 if (progress_track[i].visited)
110 progress_track[i].visited = 1;
111 printf("..%d%%", sofar);
117 write_buffer(struct dumperinfo *di, char *ptr, size_t sz)
123 maxdumpsz = di->maxiosize;
125 if (maxdumpsz == 0) /* seatbelt */
126 maxdumpsz = PAGE_SIZE;
131 len = min(maxdumpsz, sz);
136 report_progress(progress, dumpsize);
137 counter &= (1<<22) - 1;
140 wdog_kern_pat(WD_LASTVAL);
143 error = dump_append(di, ptr, 0, len);
149 panic("pa is not supported");
152 /* Check for user abort. */
157 printf(" (CTRL-C to abort) ");
164 minidumpsys(struct dumperinfo *di)
166 struct minidumphdr mdhdr;
169 vm_offset_t prev_pte = 0;
177 mips_dcache_wbinv_all();
180 /* Walk page table pages, set bits in vm_page_dump */
183 for (va = VM_MIN_KERNEL_ADDRESS; va < kernel_vm_end; va += NBPDR) {
184 ptesize += PAGE_SIZE;
185 pte = pmap_pte(kernel_pmap, va);
186 KASSERT(pte != NULL, ("pte for %jx is NULL", (uintmax_t)va));
187 for (i = 0; i < NPTEPG; i++) {
188 if (pte_test(&pte[i], PTE_V)) {
189 pa = TLBLO_PTE_TO_PA(pte[i]);
197 * Now mark pages from 0 to phys_avail[0], that's where kernel
198 * and pages allocated by pmap_steal reside
200 for (pa = 0; pa < phys_avail[0]; pa += PAGE_SIZE) {
205 /* Calculate dump size. */
207 dumpsize += round_page(msgbufp->msg_size);
208 dumpsize += round_page(sizeof(dump_avail));
209 dumpsize += round_page(BITSET_SIZE(vm_page_dump_pages));
210 VM_PAGE_DUMP_FOREACH(pa) {
211 /* Clear out undumpable pages now if needed */
213 dumpsize += PAGE_SIZE;
217 dumpsize += PAGE_SIZE;
221 /* Initialize mdhdr */
222 bzero(&mdhdr, sizeof(mdhdr));
223 strcpy(mdhdr.magic, MINIDUMP_MAGIC);
224 mdhdr.version = MINIDUMP_VERSION;
225 mdhdr.msgbufsize = msgbufp->msg_size;
226 mdhdr.bitmapsize = round_page(BITSET_SIZE(vm_page_dump_pages));
227 mdhdr.ptesize = ptesize;
228 mdhdr.kernbase = VM_MIN_KERNEL_ADDRESS;
229 mdhdr.dumpavailsize = round_page(sizeof(dump_avail));
231 dump_init_header(di, &kdh, KERNELDUMPMAGIC, KERNELDUMP_MIPS_VERSION,
234 error = dump_start(di, &kdh);
238 printf("Dumping %llu out of %ju MB:", (long long)dumpsize >> 20,
239 ptoa((uintmax_t)physmem) / 1048576);
242 bzero(tmpbuffer, sizeof(tmpbuffer));
243 bcopy(&mdhdr, tmpbuffer, sizeof(mdhdr));
244 error = write_buffer(di, tmpbuffer, PAGE_SIZE);
248 /* Dump msgbuf up front */
249 error = write_buffer(di, (char *)msgbufp->msg_ptr,
250 round_page(msgbufp->msg_size));
254 /* Dump dump_avail */
255 _Static_assert(sizeof(dump_avail) <= sizeof(tmpbuffer),
256 "Large dump_avail not handled");
257 bzero(tmpbuffer, sizeof(tmpbuffer));
258 memcpy(tmpbuffer, dump_avail, sizeof(dump_avail));
259 error = write_buffer(di, tmpbuffer, PAGE_SIZE);
264 error = write_buffer(di, (char *)vm_page_dump,
265 round_page(BITSET_SIZE(vm_page_dump_pages)));
269 /* Dump kernel page table pages */
270 for (va = VM_MIN_KERNEL_ADDRESS; va < kernel_vm_end; va += NBPDR) {
271 pte = pmap_pte(kernel_pmap, va);
272 KASSERT(pte != NULL, ("pte for %jx is NULL", (uintmax_t)va));
274 prev_pte = (vm_offset_t)pte;
277 if ((vm_offset_t)pte == (prev_pte + count * PAGE_SIZE))
280 error = write_buffer(di, (char*)prev_pte,
285 prev_pte = (vm_offset_t)pte;
291 error = write_buffer(di, (char*)prev_pte, count * PAGE_SIZE);
298 /* Dump memory chunks page by page*/
299 VM_PAGE_DUMP_FOREACH(pa) {
300 dump_va = pmap_kenter_temporary(pa, 0);
301 error = write_buffer(di, dump_va, PAGE_SIZE);
304 pmap_kenter_temporary_free(pa);
307 error = dump_finish(di, &kdh);
311 printf("\nDump complete\n");
318 if (error == ECANCELED)
319 printf("\nDump aborted\n");
320 else if (error == E2BIG || error == ENOSPC) {
321 printf("\nDump failed. Partition too small (about %lluMB were "
322 "needed this time).\n", (long long)dumpsize >> 20);
324 printf("\n** DUMP FAILED (ERROR %d) **\n", error);