]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - sys/amd64/amd64/minidump_machdep.c
MFC: re-work dump progress indicator to work better with more than few gigs
[FreeBSD/stable/8.git] / sys / amd64 / amd64 / minidump_machdep.c
1 /*-
2  * Copyright (c) 2006 Peter Wemm
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include "opt_watchdog.h"
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/conf.h>
35 #include <sys/cons.h>
36 #include <sys/kernel.h>
37 #include <sys/kerneldump.h>
38 #include <sys/msgbuf.h>
39 #ifdef SW_WATCHDOG
40 #include <sys/watchdog.h>
41 #endif
42 #include <vm/vm.h>
43 #include <vm/pmap.h>
44 #include <machine/atomic.h>
45 #include <machine/elf.h>
46 #include <machine/md_var.h>
47 #include <machine/vmparam.h>
48 #include <machine/minidump.h>
49
50 CTASSERT(sizeof(struct kerneldumpheader) == 512);
51
52 /*
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.
55  */
56 #define SIZEOF_METADATA         (64*1024)
57
58 #define MD_ALIGN(x)     (((off_t)(x) + PAGE_MASK) & ~PAGE_MASK)
59 #define DEV_ALIGN(x)    (((off_t)(x) + (DEV_BSIZE-1)) & ~(DEV_BSIZE-1))
60
61 extern uint64_t KPDPphys;
62
63 uint64_t *vm_page_dump;
64 int vm_page_dump_size;
65
66 static struct kerneldumpheader kdh;
67 static off_t dumplo;
68
69 /* Handle chunked writes. */
70 static size_t fragsz;
71 static void *dump_va;
72 static size_t counter, progress, dumpsize;
73
74 CTASSERT(sizeof(*vm_page_dump) == 8);
75
76 static int
77 is_dumpable(vm_paddr_t pa)
78 {
79         int i;
80
81         for (i = 0; dump_avail[i] != 0 || dump_avail[i + 1] != 0; i += 2) {
82                 if (pa >= dump_avail[i] && pa < dump_avail[i + 1])
83                         return (1);
84         }
85         return (0);
86 }
87
88 #define PG2MB(pgs) (((pgs) + (1 << 8) - 1) >> 8)
89
90 static int
91 blk_flush(struct dumperinfo *di)
92 {
93         int error;
94
95         if (fragsz == 0)
96                 return (0);
97
98         error = dump_write(di, dump_va, 0, dumplo, fragsz);
99         dumplo += fragsz;
100         fragsz = 0;
101         return (error);
102 }
103
104 static struct {
105         int min_per;
106         int max_per;
107         int visited;
108 } progress_track[10] = {
109         {  0,  10, 0},
110         { 10,  20, 0},
111         { 20,  30, 0},
112         { 30,  40, 0},
113         { 40,  50, 0},
114         { 50,  60, 0},
115         { 60,  70, 0},
116         { 70,  80, 0},
117         { 80,  90, 0},
118         { 90, 100, 0}
119 };
120
121 static void
122 report_progress(size_t progress, size_t dumpsize)
123 {
124         int sofar, i;
125
126         sofar = 100 - ((progress * 100) / dumpsize);
127         for (i = 0; i < 10; i++) {
128                 if (sofar < progress_track[i].min_per || sofar > progress_track[i].max_per)
129                         continue;
130                 if (progress_track[i].visited)
131                         return;
132                 progress_track[i].visited = 1;
133                 printf("..%d%%", sofar);
134                 return;
135         }
136 }
137
138 static int
139 blk_write(struct dumperinfo *di, char *ptr, vm_paddr_t pa, size_t sz)
140 {
141         size_t len;
142         int error, i, c;
143         u_int maxdumpsz;
144
145         maxdumpsz = min(di->maxiosize, MAXDUMPPGS * PAGE_SIZE);
146         if (maxdumpsz == 0)     /* seatbelt */
147                 maxdumpsz = PAGE_SIZE;
148         error = 0;
149         if ((sz % PAGE_SIZE) != 0) {
150                 printf("size not page aligned\n");
151                 return (EINVAL);
152         }
153         if (ptr != NULL && pa != 0) {
154                 printf("cant have both va and pa!\n");
155                 return (EINVAL);
156         }
157         if (pa != 0 && (((uintptr_t)ptr) % PAGE_SIZE) != 0) {
158                 printf("address not page aligned\n");
159                 return (EINVAL);
160         }
161         if (ptr != NULL) {
162                 /* If we're doing a virtual dump, flush any pre-existing pa pages */
163                 error = blk_flush(di);
164                 if (error)
165                         return (error);
166         }
167         while (sz) {
168                 len = maxdumpsz - fragsz;
169                 if (len > sz)
170                         len = sz;
171                 counter += len;
172                 progress -= len;
173                 if (counter >> 24) {
174                         report_progress(progress, dumpsize);
175                         counter &= (1<<24) - 1;
176                 }
177 #ifdef SW_WATCHDOG
178                 wdog_kern_pat(WD_LASTVAL);
179 #endif
180                 if (ptr) {
181                         error = dump_write(di, ptr, 0, dumplo, len);
182                         if (error)
183                                 return (error);
184                         dumplo += len;
185                         ptr += len;
186                         sz -= len;
187                 } else {
188                         for (i = 0; i < len; i += PAGE_SIZE)
189                                 dump_va = pmap_kenter_temporary(pa + i, (i + fragsz) >> PAGE_SHIFT);
190                         fragsz += len;
191                         pa += len;
192                         sz -= len;
193                         if (fragsz == maxdumpsz) {
194                                 error = blk_flush(di);
195                                 if (error)
196                                         return (error);
197                         }
198                 }
199
200                 /* Check for user abort. */
201                 c = cncheckc();
202                 if (c == 0x03)
203                         return (ECANCELED);
204                 if (c != -1)
205                         printf(" (CTRL-C to abort) ");
206         }
207
208         return (0);
209 }
210
211 /* A fake page table page, to avoid having to handle both 4K and 2M pages */
212 static pd_entry_t fakepd[NPDEPG];
213
214 void
215 minidumpsys(struct dumperinfo *di)
216 {
217         uint32_t pmapsize;
218         vm_offset_t va;
219         int error;
220         uint64_t bits;
221         uint64_t *pdp, *pd, *pt, pa;
222         int i, j, k, n, bit;
223         int retry_count;
224         struct minidumphdr mdhdr;
225
226         retry_count = 0;
227  retry:
228         retry_count++;
229         counter = 0;
230         /* Walk page table pages, set bits in vm_page_dump */
231         pmapsize = 0;
232         pdp = (uint64_t *)PHYS_TO_DMAP(KPDPphys);
233         for (va = VM_MIN_KERNEL_ADDRESS; va < MAX(KERNBASE + NKPT * NBPDR,
234             kernel_vm_end); ) {
235                 /*
236                  * We always write a page, even if it is zero. Each
237                  * page written corresponds to 1GB of space
238                  */
239                 pmapsize += PAGE_SIZE;
240                 i = (va >> PDPSHIFT) & ((1ul << NPDPEPGSHIFT) - 1);
241                 if ((pdp[i] & PG_V) == 0) {
242                         va += NBPDP;
243                         continue;
244                 }
245
246                 /*
247                  * 1GB page is represented as 512 2MB pages in a dump.
248                  */
249                 if ((pdp[i] & PG_PS) != 0) {
250                         va += NBPDP;
251                         pa = pdp[i] & PG_PS_FRAME;
252                         for (n = 0; n < NPDEPG * NPTEPG; n++) {
253                                 if (is_dumpable(pa))
254                                         dump_add_page(pa);
255                                 pa += PAGE_SIZE;
256                         }
257                         continue;
258                 }
259
260                 pd = (uint64_t *)PHYS_TO_DMAP(pdp[i] & PG_FRAME);
261                 for (n = 0; n < NPDEPG; n++, va += NBPDR) {
262                         j = (va >> PDRSHIFT) & ((1ul << NPDEPGSHIFT) - 1);
263
264                         if ((pd[j] & PG_V) == 0)
265                                 continue;
266
267                         if ((pd[j] & PG_PS) != 0) {
268                                 /* This is an entire 2M page. */
269                                 pa = pd[j] & PG_PS_FRAME;
270                                 for (k = 0; k < NPTEPG; k++) {
271                                         if (is_dumpable(pa))
272                                                 dump_add_page(pa);
273                                         pa += PAGE_SIZE;
274                                 }
275                                 continue;
276                         }
277
278                         pa = pd[j] & PG_FRAME;
279                         /* set bit for this PTE page */
280                         if (is_dumpable(pa))
281                                 dump_add_page(pa);
282                         /* and for each valid page in this 2MB block */
283                         pt = (uint64_t *)PHYS_TO_DMAP(pd[j] & PG_FRAME);
284                         for (k = 0; k < NPTEPG; k++) {
285                                 if ((pt[k] & PG_V) == 0)
286                                         continue;
287                                 pa = pt[k] & PG_FRAME;
288                                 if (is_dumpable(pa))
289                                         dump_add_page(pa);
290                         }
291                 }
292         }
293
294         /* Calculate dump size. */
295         dumpsize = pmapsize;
296         dumpsize += round_page(msgbufp->msg_size);
297         dumpsize += round_page(vm_page_dump_size);
298         for (i = 0; i < vm_page_dump_size / sizeof(*vm_page_dump); i++) {
299                 bits = vm_page_dump[i];
300                 while (bits) {
301                         bit = bsfq(bits);
302                         pa = (((uint64_t)i * sizeof(*vm_page_dump) * NBBY) + bit) * PAGE_SIZE;
303                         /* Clear out undumpable pages now if needed */
304                         if (is_dumpable(pa)) {
305                                 dumpsize += PAGE_SIZE;
306                         } else {
307                                 dump_drop_page(pa);
308                         }
309                         bits &= ~(1ul << bit);
310                 }
311         }
312         dumpsize += PAGE_SIZE;
313
314         /* Determine dump offset on device. */
315         if (di->mediasize < SIZEOF_METADATA + dumpsize + sizeof(kdh) * 2) {
316                 error = E2BIG;
317                 goto fail;
318         }
319         dumplo = di->mediaoffset + di->mediasize - dumpsize;
320         dumplo -= sizeof(kdh) * 2;
321         progress = dumpsize;
322
323         /* Initialize mdhdr */
324         bzero(&mdhdr, sizeof(mdhdr));
325         strcpy(mdhdr.magic, MINIDUMP_MAGIC);
326         mdhdr.version = MINIDUMP_VERSION;
327         mdhdr.msgbufsize = msgbufp->msg_size;
328         mdhdr.bitmapsize = vm_page_dump_size;
329         mdhdr.pmapsize = pmapsize;
330         mdhdr.kernbase = VM_MIN_KERNEL_ADDRESS;
331         mdhdr.dmapbase = DMAP_MIN_ADDRESS;
332         mdhdr.dmapend = DMAP_MAX_ADDRESS;
333
334         mkdumpheader(&kdh, KERNELDUMPMAGIC, KERNELDUMP_AMD64_VERSION, dumpsize, di->blocksize);
335
336         printf("Dumping %llu out of %ju MB:", (long long)dumpsize >> 20,
337             ptoa((uintmax_t)physmem) / 1048576);
338
339         /* Dump leader */
340         error = dump_write(di, &kdh, 0, dumplo, sizeof(kdh));
341         if (error)
342                 goto fail;
343         dumplo += sizeof(kdh);
344
345         /* Dump my header */
346         bzero(&fakepd, sizeof(fakepd));
347         bcopy(&mdhdr, &fakepd, sizeof(mdhdr));
348         error = blk_write(di, (char *)&fakepd, 0, PAGE_SIZE);
349         if (error)
350                 goto fail;
351
352         /* Dump msgbuf up front */
353         error = blk_write(di, (char *)msgbufp->msg_ptr, 0, round_page(msgbufp->msg_size));
354         if (error)
355                 goto fail;
356
357         /* Dump bitmap */
358         error = blk_write(di, (char *)vm_page_dump, 0, round_page(vm_page_dump_size));
359         if (error)
360                 goto fail;
361
362         /* Dump kernel page directory pages */
363         bzero(fakepd, sizeof(fakepd));
364         pdp = (uint64_t *)PHYS_TO_DMAP(KPDPphys);
365         for (va = VM_MIN_KERNEL_ADDRESS; va < MAX(KERNBASE + NKPT * NBPDR,
366             kernel_vm_end); va += NBPDP) {
367                 i = (va >> PDPSHIFT) & ((1ul << NPDPEPGSHIFT) - 1);
368
369                 /* We always write a page, even if it is zero */
370                 if ((pdp[i] & PG_V) == 0) {
371                         error = blk_write(di, (char *)&fakepd, 0, PAGE_SIZE);
372                         if (error)
373                                 goto fail;
374                         /* flush, in case we reuse fakepd in the same block */
375                         error = blk_flush(di);
376                         if (error)
377                                 goto fail;
378                         continue;
379                 }
380
381                 /* 1GB page is represented as 512 2MB pages in a dump */
382                 if ((pdp[i] & PG_PS) != 0) {
383                         /* PDPE and PDP have identical layout in this case */
384                         fakepd[0] = pdp[i];
385                         for (j = 1; j < NPDEPG; j++)
386                                 fakepd[j] = fakepd[j - 1] + NBPDR;
387                         error = blk_write(di, (char *)&fakepd, 0, PAGE_SIZE);
388                         if (error)
389                                 goto fail;
390                         /* flush, in case we reuse fakepd in the same block */
391                         error = blk_flush(di);
392                         if (error)
393                                 goto fail;
394                         bzero(fakepd, sizeof(fakepd));
395                         continue;
396                 }
397
398                 pd = (uint64_t *)PHYS_TO_DMAP(pdp[i] & PG_FRAME);
399                 error = blk_write(di, (char *)pd, 0, PAGE_SIZE);
400                 if (error)
401                         goto fail;
402                 error = blk_flush(di);
403                 if (error)
404                         goto fail;
405         }
406
407         /* Dump memory chunks */
408         /* XXX cluster it up and use blk_dump() */
409         for (i = 0; i < vm_page_dump_size / sizeof(*vm_page_dump); i++) {
410                 bits = vm_page_dump[i];
411                 while (bits) {
412                         bit = bsfq(bits);
413                         pa = (((uint64_t)i * sizeof(*vm_page_dump) * NBBY) + bit) * PAGE_SIZE;
414                         error = blk_write(di, 0, pa, PAGE_SIZE);
415                         if (error)
416                                 goto fail;
417                         bits &= ~(1ul << bit);
418                 }
419         }
420
421         error = blk_flush(di);
422         if (error)
423                 goto fail;
424
425         /* Dump trailer */
426         error = dump_write(di, &kdh, 0, dumplo, sizeof(kdh));
427         if (error)
428                 goto fail;
429         dumplo += sizeof(kdh);
430
431         /* Signal completion, signoff and exit stage left. */
432         dump_write(di, NULL, 0, 0, 0);
433         printf("\nDump complete\n");
434         return;
435
436  fail:
437         if (error < 0)
438                 error = -error;
439
440         printf("\n");
441         if (error == ENOSPC) {
442                 printf("Dump map grown while dumping. ");
443                 if (retry_count < 5) {
444                         printf("Retrying...\n");
445                         goto retry;
446                 }
447                 printf("Dump failed.\n");
448         }
449         else if (error == ECANCELED)
450                 printf("Dump aborted\n");
451         else if (error == E2BIG)
452                 printf("Dump failed. Partition too small.\n");
453         else
454                 printf("** DUMP FAILED (ERROR %d) **\n", error);
455 }
456
457 void
458 dump_add_page(vm_paddr_t pa)
459 {
460         int idx, bit;
461
462         pa >>= PAGE_SHIFT;
463         idx = pa >> 6;          /* 2^6 = 64 */
464         bit = pa & 63;
465         atomic_set_long(&vm_page_dump[idx], 1ul << bit);
466 }
467
468 void
469 dump_drop_page(vm_paddr_t pa)
470 {
471         int idx, bit;
472
473         pa >>= PAGE_SHIFT;
474         idx = pa >> 6;          /* 2^6 = 64 */
475         bit = pa & 63;
476         atomic_clear_long(&vm_page_dump[idx], 1ul << bit);
477 }