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