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