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