]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/mips/mips/minidump_machdep.c
MFhead@r324837
[FreeBSD/FreeBSD.git] / sys / mips / mips / minidump_machdep.c
1 /*-
2  * Copyright (c) 2010 Oleksandr Tymoshenko <gonzo@freebsd.org>
3  * Copyright (c) 2008 Semihalf, Grzegorz Bernacki
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
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.
15  *
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.
26  *
27  * from: FreeBSD: src/sys/arm/arm/minidump_machdep.c v214223
28  */
29
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/conf.h>
36 #include <sys/cons.h>
37 #include <sys/kernel.h>
38 #include <sys/kerneldump.h>
39 #include <sys/msgbuf.h>
40 #include <sys/watchdog.h>
41 #include <sys/vmmeter.h>
42 #include <vm/vm.h>
43 #include <vm/pmap.h>
44 #include <vm/vm_page.h>
45 #include <vm/vm_phys.h>
46 #include <machine/atomic.h>
47 #include <machine/elf.h>
48 #include <machine/md_var.h>
49 #include <machine/vmparam.h>
50 #include <machine/minidump.h>
51 #include <machine/cache.h>
52
53 CTASSERT(sizeof(struct kerneldumpheader) == 512);
54
55 uint32_t *vm_page_dump;
56 int vm_page_dump_size;
57
58 static struct kerneldumpheader kdh;
59
60 /* Handle chunked writes. */
61 static uint64_t counter, progress, dumpsize;
62 /* Just auxiliary bufffer */
63 static char tmpbuffer[PAGE_SIZE];
64
65 extern pd_entry_t *kernel_segmap;
66
67 CTASSERT(sizeof(*vm_page_dump) == 4);
68
69 static int
70 is_dumpable(vm_paddr_t pa)
71 {
72         vm_page_t m;
73         int i;
74
75         if ((m = vm_phys_paddr_to_vm_page(pa)) != NULL)
76                 return ((m->flags & PG_NODUMP) == 0);
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])
79                         return (1);
80         }
81         return (0);
82 }
83
84 void
85 dump_add_page(vm_paddr_t pa)
86 {
87         int idx, bit;
88
89         pa >>= PAGE_SHIFT;
90         idx = pa >> 5;          /* 2^5 = 32 */
91         bit = pa & 31;
92         atomic_set_int(&vm_page_dump[idx], 1ul << bit);
93 }
94
95 void
96 dump_drop_page(vm_paddr_t pa)
97 {
98         int idx, bit;
99
100         pa >>= PAGE_SHIFT;
101         idx = pa >> 5;          /* 2^5 = 32 */
102         bit = pa & 31;
103         atomic_clear_int(&vm_page_dump[idx], 1ul << bit);
104 }
105
106 static struct {
107         int min_per;
108         int max_per;
109         int visited;
110 } progress_track[10] = {
111         {  0,  10, 0},
112         { 10,  20, 0},
113         { 20,  30, 0},
114         { 30,  40, 0},
115         { 40,  50, 0},
116         { 50,  60, 0},
117         { 60,  70, 0},
118         { 70,  80, 0},
119         { 80,  90, 0},
120         { 90, 100, 0}
121 };
122
123 static void
124 report_progress(uint64_t progress, uint64_t dumpsize)
125 {
126         int sofar, i;
127
128         sofar = 100 - ((progress * 100) / dumpsize);
129         for (i = 0; i < nitems(progress_track); i++) {
130                 if (sofar < progress_track[i].min_per ||
131                     sofar > progress_track[i].max_per)
132                         continue;
133                 if (progress_track[i].visited)
134                         return;
135                 progress_track[i].visited = 1;
136                 printf("..%d%%", sofar);
137                 return;
138         }
139 }
140
141 static int
142 write_buffer(struct dumperinfo *di, char *ptr, size_t sz)
143 {
144         size_t len;
145         int error, c;
146         u_int maxdumpsz;
147
148         maxdumpsz = di->maxiosize;
149
150         if (maxdumpsz == 0)     /* seatbelt */
151                 maxdumpsz = PAGE_SIZE;
152
153         error = 0;
154
155         while (sz) {
156                 len = min(maxdumpsz, sz);
157                 counter += len;
158                 progress -= len;
159
160                 if (counter >> 22) {
161                         report_progress(progress, dumpsize);
162                         counter &= (1<<22) - 1;
163                 }
164
165                 wdog_kern_pat(WD_LASTVAL);
166
167                 if (ptr) {
168                         error = dump_append(di, ptr, 0, len);
169                         if (error)
170                                 return (error);
171                         ptr += len;
172                         sz -= len;
173                 } else {
174                         panic("pa is not supported");
175                 }
176
177                 /* Check for user abort. */
178                 c = cncheckc();
179                 if (c == 0x03)
180                         return (ECANCELED);
181                 if (c != -1)
182                         printf(" (CTRL-C to abort) ");
183         }
184
185         return (0);
186 }
187
188 int
189 minidumpsys(struct dumperinfo *di)
190 {
191         struct minidumphdr mdhdr;
192         uint32_t ptesize;
193         uint32_t bits;
194         vm_paddr_t pa;
195         vm_offset_t prev_pte = 0;
196         uint32_t count = 0;
197         vm_offset_t va;
198         pt_entry_t *pte;
199         int i, bit, error;
200         void *dump_va;
201
202         /* Flush cache */
203         mips_dcache_wbinv_all();
204
205         counter = 0;
206         /* Walk page table pages, set bits in vm_page_dump */
207         ptesize = 0;
208
209         for (va = VM_MIN_KERNEL_ADDRESS; va < kernel_vm_end; va += NBPDR) {
210                 ptesize += PAGE_SIZE;
211                 pte = pmap_pte(kernel_pmap, va);
212                 KASSERT(pte != NULL, ("pte for %jx is NULL", (uintmax_t)va));
213                 for (i = 0; i < NPTEPG; i++) {
214                         if (pte_test(&pte[i], PTE_V)) {
215                                 pa = TLBLO_PTE_TO_PA(pte[i]);
216                                 if (is_dumpable(pa))
217                                         dump_add_page(pa);
218                         }
219                 }
220         }
221
222         /*
223          * Now mark pages from 0 to phys_avail[0], that's where kernel 
224          * and pages allocated by pmap_steal reside
225          */
226         for (pa = 0; pa < phys_avail[0]; pa += PAGE_SIZE) {
227                 if (is_dumpable(pa))
228                         dump_add_page(pa);
229         }
230
231         /* Calculate dump size. */
232         dumpsize = ptesize;
233         dumpsize += round_page(msgbufp->msg_size);
234         dumpsize += round_page(vm_page_dump_size);
235         for (i = 0; i < vm_page_dump_size / sizeof(*vm_page_dump); i++) {
236                 bits = vm_page_dump[i];
237                 while (bits) {
238                         bit = ffs(bits) - 1;
239                         pa = (((uint64_t)i * sizeof(*vm_page_dump) * NBBY) +
240                             bit) * PAGE_SIZE;
241                         /* Clear out undumpable pages now if needed */
242                         if (is_dumpable(pa))
243                                 dumpsize += PAGE_SIZE;
244                         else
245                                 dump_drop_page(pa);
246                         bits &= ~(1ul << bit);
247                 }
248         }
249         dumpsize += PAGE_SIZE;
250
251         progress = dumpsize;
252
253         /* Initialize mdhdr */
254         bzero(&mdhdr, sizeof(mdhdr));
255         strcpy(mdhdr.magic, MINIDUMP_MAGIC);
256         mdhdr.version = MINIDUMP_VERSION;
257         mdhdr.msgbufsize = msgbufp->msg_size;
258         mdhdr.bitmapsize = vm_page_dump_size;
259         mdhdr.ptesize = ptesize;
260         mdhdr.kernbase = VM_MIN_KERNEL_ADDRESS;
261
262         dump_init_header(di, &kdh, KERNELDUMPMAGIC, KERNELDUMP_MIPS_VERSION,
263             dumpsize);
264
265         printf("Dumping %llu out of %ju MB:", (long long)dumpsize >> 20,
266             ptoa((uintmax_t)physmem) / 1048576);
267
268         error = dump_start(di, &kdh);
269         if (error != 0)
270                 goto fail;
271
272         /* Dump my header */
273         bzero(tmpbuffer, sizeof(tmpbuffer));
274         bcopy(&mdhdr, tmpbuffer, sizeof(mdhdr));
275         error = write_buffer(di, tmpbuffer, PAGE_SIZE);
276         if (error)
277                 goto fail;
278
279         /* Dump msgbuf up front */
280         error = write_buffer(di, (char *)msgbufp->msg_ptr, 
281             round_page(msgbufp->msg_size));
282         if (error)
283                 goto fail;
284
285         /* Dump bitmap */
286         error = write_buffer(di, (char *)vm_page_dump,
287             round_page(vm_page_dump_size));
288         if (error)
289                 goto fail;
290
291         /* Dump kernel page table pages */
292         for (va = VM_MIN_KERNEL_ADDRESS; va < kernel_vm_end; va += NBPDR) {
293                 pte = pmap_pte(kernel_pmap, va);
294                 KASSERT(pte != NULL, ("pte for %jx is NULL", (uintmax_t)va));
295                 if (!count) {
296                         prev_pte = (vm_offset_t)pte;
297                         count++;
298                 } else {
299                         if ((vm_offset_t)pte == (prev_pte + count * PAGE_SIZE))
300                                 count++;
301                         else {
302                                 error = write_buffer(di, (char*)prev_pte,
303                                     count * PAGE_SIZE);
304                                 if (error)
305                                         goto fail;
306                                 count = 1;
307                                 prev_pte = (vm_offset_t)pte;
308                         }
309                 }
310         }
311
312         if (count) {
313                 error = write_buffer(di, (char*)prev_pte, count * PAGE_SIZE);
314                 if (error)
315                         goto fail;
316                 count = 0;
317                 prev_pte = 0;
318         }
319
320         /* Dump memory chunks  page by page*/
321         for (i = 0; i < vm_page_dump_size / sizeof(*vm_page_dump); i++) {
322                 bits = vm_page_dump[i];
323                 while (bits) {
324                         bit = ffs(bits) - 1;
325                         pa = (((uint64_t)i * sizeof(*vm_page_dump) * NBBY) +
326                             bit) * PAGE_SIZE;
327                         dump_va = pmap_kenter_temporary(pa, 0);
328                         error = write_buffer(di, dump_va, PAGE_SIZE);
329                         if (error)
330                                 goto fail;
331                         pmap_kenter_temporary_free(pa);
332                         bits &= ~(1ul << bit);
333                 }
334         }
335
336         error = dump_finish(di, &kdh);
337         if (error != 0)
338                 goto fail;
339
340         printf("\nDump complete\n");
341         return (0);
342
343 fail:
344         if (error < 0)
345                 error = -error;
346
347         if (error == ECANCELED)
348                 printf("\nDump aborted\n");
349         else if (error == E2BIG || error == ENOSPC)
350                 printf("\nDump failed. Partition too small.\n");
351         else
352                 printf("\n** DUMP FAILED (ERROR %d) **\n", error);
353         return (error);
354 }