2 * Copyright (c) 2010 Oleksandr Tymoshenko <gonzo@freebsd.org>
3 * Copyright (c) 2008 Semihalf, Grzegorz Bernacki
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 * from: FreeBSD: src/sys/arm/arm/minidump_machdep.c v214223
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/kerneldump.h>
39 #include <sys/msgbuf.h>
42 #include <machine/pmap.h>
43 #include <machine/atomic.h>
44 #include <machine/elf.h>
45 #include <machine/md_var.h>
46 #include <machine/vmparam.h>
47 #include <machine/minidump.h>
48 #include <machine/cache.h>
50 CTASSERT(sizeof(struct kerneldumpheader) == 512);
53 * Don't touch the first SIZEOF_METADATA bytes on the dump device. This
54 * is to protect us from metadata and to protect metadata from us.
56 #define SIZEOF_METADATA (64*1024)
58 uint32_t *vm_page_dump;
59 int vm_page_dump_size;
61 static struct kerneldumpheader kdh;
63 static off_t origdumplo;
65 /* Handle chunked writes. */
66 static uint64_t counter, progress;
67 /* Just auxiliary bufffer */
68 static char tmpbuffer[PAGE_SIZE];
70 extern pd_entry_t *kernel_segmap;
72 CTASSERT(sizeof(*vm_page_dump) == 4);
75 is_dumpable(vm_paddr_t pa)
79 for (i = 0; dump_avail[i] != 0 || dump_avail[i + 1] != 0; i += 2) {
80 if (pa >= dump_avail[i] && pa < dump_avail[i + 1])
87 dump_add_page(vm_paddr_t pa)
92 idx = pa >> 5; /* 2^5 = 32 */
94 atomic_set_int(&vm_page_dump[idx], 1ul << bit);
98 dump_drop_page(vm_paddr_t pa)
103 idx = pa >> 5; /* 2^5 = 32 */
105 atomic_clear_int(&vm_page_dump[idx], 1ul << bit);
108 #define PG2MB(pgs) (((pgs) + (1 << 8) - 1) >> 8)
111 write_buffer(struct dumperinfo *di, char *ptr, size_t sz)
117 maxdumpsz = di->maxiosize;
119 if (maxdumpsz == 0) /* seatbelt */
120 maxdumpsz = PAGE_SIZE;
125 len = min(maxdumpsz, sz);
130 printf(" %jd", PG2MB(progress >> PAGE_SHIFT));
131 counter &= (1<<22) - 1;
135 error = dump_write(di, ptr, 0, dumplo, len);
142 panic("pa is not supported");
145 /* Check for user abort. */
150 printf(" (CTRL-C to abort) ");
157 minidumpsys(struct dumperinfo *di)
159 struct minidumphdr mdhdr;
164 vm_offset_t prev_pte = 0;
172 mips_dcache_wbinv_all();
175 /* Walk page table pages, set bits in vm_page_dump */
178 for (va = VM_MIN_KERNEL_ADDRESS; va < kernel_vm_end; va += NBPDR) {
179 ptesize += PAGE_SIZE;
180 pte = pmap_pte(kernel_pmap, va);
181 KASSERT(pte != NULL, ("pte for %jx is NULL", (uintmax_t)va));
182 for (i = 0; i < NPTEPG; i++) {
183 if (pte_test(&pte[i], PTE_V)) {
184 pa = TLBLO_PTE_TO_PA(pte[i]);
192 * Now mark pages from 0 to phys_avail[0], that's where kernel
193 * and pages allocated by pmap_steal reside
195 for (pa = 0; pa < phys_avail[0]; pa += PAGE_SIZE) {
200 /* Calculate dump size. */
202 dumpsize += round_page(msgbufp->msg_size);
203 dumpsize += round_page(vm_page_dump_size);
205 for (i = 0; i < vm_page_dump_size / sizeof(*vm_page_dump); i++) {
206 bits = vm_page_dump[i];
209 pa = (((uint64_t)i * sizeof(*vm_page_dump) * NBBY) +
211 /* Clear out undumpable pages now if needed */
213 dumpsize += PAGE_SIZE;
216 bits &= ~(1ul << bit);
220 dumpsize += PAGE_SIZE;
222 /* Determine dump offset on device. */
223 if (di->mediasize < SIZEOF_METADATA + dumpsize + sizeof(kdh) * 2) {
228 origdumplo = dumplo = di->mediaoffset + di->mediasize - dumpsize;
229 dumplo -= sizeof(kdh) * 2;
232 /* Initialize mdhdr */
233 bzero(&mdhdr, sizeof(mdhdr));
234 strcpy(mdhdr.magic, MINIDUMP_MAGIC);
235 mdhdr.version = MINIDUMP_VERSION;
236 mdhdr.msgbufsize = msgbufp->msg_size;
237 mdhdr.bitmapsize = vm_page_dump_size;
238 mdhdr.ptesize = ptesize;
239 mdhdr.kernbase = VM_MIN_KERNEL_ADDRESS;
241 mkdumpheader(&kdh, KERNELDUMPMAGIC, KERNELDUMP_MIPS_VERSION, dumpsize,
244 printf("Physical memory: %ju MB\n",
245 (uintmax_t)ptoa((uintmax_t)physmem) / 1048576);
246 printf("Dumping %llu MB:", (long long)dumpsize >> 20);
249 error = dump_write(di, &kdh, 0, dumplo, sizeof(kdh));
252 dumplo += sizeof(kdh);
255 bzero(tmpbuffer, sizeof(tmpbuffer));
256 bcopy(&mdhdr, tmpbuffer, sizeof(mdhdr));
257 error = write_buffer(di, tmpbuffer, PAGE_SIZE);
261 /* Dump msgbuf up front */
262 error = write_buffer(di, (char *)msgbufp->msg_ptr,
263 round_page(msgbufp->msg_size));
268 error = write_buffer(di, (char *)vm_page_dump,
269 round_page(vm_page_dump_size));
273 /* Dump kernel page table pages */
274 for (va = VM_MIN_KERNEL_ADDRESS; va < kernel_vm_end; va += NBPDR) {
275 pte = pmap_pte(kernel_pmap, va);
276 KASSERT(pte != NULL, ("pte for %jx is NULL", (uintmax_t)va));
278 prev_pte = (vm_offset_t)pte;
282 if ((vm_offset_t)pte == (prev_pte + count * PAGE_SIZE))
285 error = write_buffer(di, (char*)prev_pte,
290 prev_pte = (vm_offset_t)pte;
296 error = write_buffer(di, (char*)prev_pte, count * PAGE_SIZE);
303 /* Dump memory chunks page by page*/
304 for (i = 0; i < vm_page_dump_size / sizeof(*vm_page_dump); i++) {
305 bits = vm_page_dump[i];
308 pa = (((uint64_t)i * sizeof(*vm_page_dump) * NBBY) +
310 dump_va = pmap_kenter_temporary(pa, 0);
311 error = write_buffer(di, dump_va, PAGE_SIZE);
314 pmap_kenter_temporary_free(pa);
315 bits &= ~(1ul << bit);
320 error = dump_write(di, &kdh, 0, dumplo, sizeof(kdh));
323 dumplo += sizeof(kdh);
325 /* Signal completion, signoff and exit stage left. */
326 dump_write(di, NULL, 0, 0, 0);
327 printf("\nDump complete\n");
334 if (error == ECANCELED)
335 printf("\nDump aborted\n");
336 else if (error == ENOSPC)
337 printf("\nDump failed. Partition too small.\n");
339 printf("\n** DUMP FAILED (ERROR %d) **\n", error);