]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - sys/amd64/amd64/minidump_machdep.c
Copy head (r256279) to stable/10 as part of the 10.0-RELEASE cycle.
[FreeBSD/stable/10.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_pmap.h"
31 #include "opt_watchdog.h"
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/vm_param.h>
43 #include <vm/vm_page.h>
44 #include <vm/vm_phys.h>
45 #include <vm/pmap.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
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 #define MD_ALIGN(x)     (((off_t)(x) + PAGE_MASK) & ~PAGE_MASK)
61 #define DEV_ALIGN(x)    (((off_t)(x) + (DEV_BSIZE-1)) & ~(DEV_BSIZE-1))
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         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 #define PG2MB(pgs) (((pgs) + (1 << 8) - 1) >> 8)
92
93 static int
94 blk_flush(struct dumperinfo *di)
95 {
96         int error;
97
98         if (fragsz == 0)
99                 return (0);
100
101         error = dump_write(di, dump_va, 0, dumplo, fragsz);
102         dumplo += fragsz;
103         fragsz = 0;
104         return (error);
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(size_t progress, size_t dumpsize)
126 {
127         int sofar, i;
128
129         sofar = 100 - ((progress * 100) / dumpsize);
130         for (i = 0; i < 10; i++) {
131                 if (sofar < progress_track[i].min_per || 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 blk_write(struct dumperinfo *di, char *ptr, vm_paddr_t pa, size_t sz)
143 {
144         size_t len;
145         int error, i, c;
146         u_int maxdumpsz;
147
148         maxdumpsz = min(di->maxiosize, MAXDUMPPGS * PAGE_SIZE);
149         if (maxdumpsz == 0)     /* seatbelt */
150                 maxdumpsz = PAGE_SIZE;
151         error = 0;
152         if ((sz % PAGE_SIZE) != 0) {
153                 printf("size not page aligned\n");
154                 return (EINVAL);
155         }
156         if (ptr != NULL && pa != 0) {
157                 printf("cant have both va and pa!\n");
158                 return (EINVAL);
159         }
160         if (pa != 0 && (((uintptr_t)ptr) % PAGE_SIZE) != 0) {
161                 printf("address not page aligned\n");
162                 return (EINVAL);
163         }
164         if (ptr != NULL) {
165                 /* If we're doing a virtual dump, flush any pre-existing pa pages */
166                 error = blk_flush(di);
167                 if (error)
168                         return (error);
169         }
170         while (sz) {
171                 len = maxdumpsz - fragsz;
172                 if (len > sz)
173                         len = sz;
174                 counter += len;
175                 progress -= len;
176                 if (counter >> 24) {
177                         report_progress(progress, dumpsize);
178                         counter &= (1<<24) - 1;
179                 }
180
181                 wdog_kern_pat(WD_LASTVAL);
182
183                 if (ptr) {
184                         error = dump_write(di, ptr, 0, dumplo, len);
185                         if (error)
186                                 return (error);
187                         dumplo += len;
188                         ptr += len;
189                         sz -= len;
190                 } else {
191                         for (i = 0; i < len; i += PAGE_SIZE)
192                                 dump_va = pmap_kenter_temporary(pa + i, (i + fragsz) >> PAGE_SHIFT);
193                         fragsz += len;
194                         pa += len;
195                         sz -= len;
196                         if (fragsz == maxdumpsz) {
197                                 error = blk_flush(di);
198                                 if (error)
199                                         return (error);
200                         }
201                 }
202
203                 /* Check for user abort. */
204                 c = cncheckc();
205                 if (c == 0x03)
206                         return (ECANCELED);
207                 if (c != -1)
208                         printf(" (CTRL-C to abort) ");
209         }
210
211         return (0);
212 }
213
214 /* A fake page table page, to avoid having to handle both 4K and 2M pages */
215 static pd_entry_t fakepd[NPDEPG];
216
217 void
218 minidumpsys(struct dumperinfo *di)
219 {
220         uint32_t pmapsize;
221         vm_offset_t va;
222         int error;
223         uint64_t bits;
224         uint64_t *pml4, *pdp, *pd, *pt, pa;
225         int i, ii, j, k, n, bit;
226         int retry_count;
227         struct minidumphdr mdhdr;
228
229         retry_count = 0;
230  retry:
231         retry_count++;
232         counter = 0;
233         /* Walk page table pages, set bits in vm_page_dump */
234         pmapsize = 0;
235         for (va = VM_MIN_KERNEL_ADDRESS; va < MAX(KERNBASE + nkpt * NBPDR,
236             kernel_vm_end); ) {
237                 /*
238                  * We always write a page, even if it is zero. Each
239                  * page written corresponds to 1GB of space
240                  */
241                 pmapsize += PAGE_SIZE;
242                 ii = (va >> PML4SHIFT) & ((1ul << NPML4EPGSHIFT) - 1);
243                 pml4 = (uint64_t *)PHYS_TO_DMAP(KPML4phys) + ii;
244                 pdp = (uint64_t *)PHYS_TO_DMAP(*pml4 & PG_FRAME);
245                 i = (va >> PDPSHIFT) & ((1ul << NPDPEPGSHIFT) - 1);
246                 if ((pdp[i] & PG_V) == 0) {
247                         va += NBPDP;
248                         continue;
249                 }
250
251                 /*
252                  * 1GB page is represented as 512 2MB pages in a dump.
253                  */
254                 if ((pdp[i] & PG_PS) != 0) {
255                         va += NBPDP;
256                         pa = pdp[i] & PG_PS_FRAME;
257                         for (n = 0; n < NPDEPG * NPTEPG; n++) {
258                                 if (is_dumpable(pa))
259                                         dump_add_page(pa);
260                                 pa += PAGE_SIZE;
261                         }
262                         continue;
263                 }
264
265                 pd = (uint64_t *)PHYS_TO_DMAP(pdp[i] & PG_FRAME);
266                 for (n = 0; n < NPDEPG; n++, va += NBPDR) {
267                         j = (va >> PDRSHIFT) & ((1ul << NPDEPGSHIFT) - 1);
268
269                         if ((pd[j] & PG_V) == 0)
270                                 continue;
271
272                         if ((pd[j] & PG_PS) != 0) {
273                                 /* This is an entire 2M page. */
274                                 pa = pd[j] & PG_PS_FRAME;
275                                 for (k = 0; k < NPTEPG; k++) {
276                                         if (is_dumpable(pa))
277                                                 dump_add_page(pa);
278                                         pa += PAGE_SIZE;
279                                 }
280                                 continue;
281                         }
282
283                         pa = pd[j] & PG_FRAME;
284                         /* set bit for this PTE page */
285                         if (is_dumpable(pa))
286                                 dump_add_page(pa);
287                         /* and for each valid page in this 2MB block */
288                         pt = (uint64_t *)PHYS_TO_DMAP(pd[j] & PG_FRAME);
289                         for (k = 0; k < NPTEPG; k++) {
290                                 if ((pt[k] & PG_V) == 0)
291                                         continue;
292                                 pa = pt[k] & PG_FRAME;
293                                 if (is_dumpable(pa))
294                                         dump_add_page(pa);
295                         }
296                 }
297         }
298
299         /* Calculate dump size. */
300         dumpsize = pmapsize;
301         dumpsize += round_page(msgbufp->msg_size);
302         dumpsize += round_page(vm_page_dump_size);
303         for (i = 0; i < vm_page_dump_size / sizeof(*vm_page_dump); i++) {
304                 bits = vm_page_dump[i];
305                 while (bits) {
306                         bit = bsfq(bits);
307                         pa = (((uint64_t)i * sizeof(*vm_page_dump) * NBBY) + bit) * PAGE_SIZE;
308                         /* Clear out undumpable pages now if needed */
309                         if (is_dumpable(pa)) {
310                                 dumpsize += PAGE_SIZE;
311                         } else {
312                                 dump_drop_page(pa);
313                         }
314                         bits &= ~(1ul << bit);
315                 }
316         }
317         dumpsize += PAGE_SIZE;
318
319         /* Determine dump offset on device. */
320         if (di->mediasize < SIZEOF_METADATA + dumpsize + sizeof(kdh) * 2) {
321                 error = E2BIG;
322                 goto fail;
323         }
324         dumplo = di->mediaoffset + di->mediasize - dumpsize;
325         dumplo -= sizeof(kdh) * 2;
326         progress = dumpsize;
327
328         /* Initialize mdhdr */
329         bzero(&mdhdr, sizeof(mdhdr));
330         strcpy(mdhdr.magic, MINIDUMP_MAGIC);
331         mdhdr.version = MINIDUMP_VERSION;
332         mdhdr.msgbufsize = msgbufp->msg_size;
333         mdhdr.bitmapsize = vm_page_dump_size;
334         mdhdr.pmapsize = pmapsize;
335         mdhdr.kernbase = VM_MIN_KERNEL_ADDRESS;
336         mdhdr.dmapbase = DMAP_MIN_ADDRESS;
337         mdhdr.dmapend = DMAP_MAX_ADDRESS;
338
339         mkdumpheader(&kdh, KERNELDUMPMAGIC, KERNELDUMP_AMD64_VERSION, dumpsize, di->blocksize);
340
341         printf("Dumping %llu out of %ju MB:", (long long)dumpsize >> 20,
342             ptoa((uintmax_t)physmem) / 1048576);
343
344         /* Dump leader */
345         error = dump_write(di, &kdh, 0, dumplo, sizeof(kdh));
346         if (error)
347                 goto fail;
348         dumplo += sizeof(kdh);
349
350         /* Dump my header */
351         bzero(&fakepd, sizeof(fakepd));
352         bcopy(&mdhdr, &fakepd, sizeof(mdhdr));
353         error = blk_write(di, (char *)&fakepd, 0, PAGE_SIZE);
354         if (error)
355                 goto fail;
356
357         /* Dump msgbuf up front */
358         error = blk_write(di, (char *)msgbufp->msg_ptr, 0, round_page(msgbufp->msg_size));
359         if (error)
360                 goto fail;
361
362         /* Dump bitmap */
363         error = blk_write(di, (char *)vm_page_dump, 0, round_page(vm_page_dump_size));
364         if (error)
365                 goto fail;
366
367         /* Dump kernel page directory pages */
368         bzero(fakepd, sizeof(fakepd));
369         for (va = VM_MIN_KERNEL_ADDRESS; va < MAX(KERNBASE + nkpt * NBPDR,
370             kernel_vm_end); va += NBPDP) {
371                 ii = (va >> PML4SHIFT) & ((1ul << NPML4EPGSHIFT) - 1);
372                 pml4 = (uint64_t *)PHYS_TO_DMAP(KPML4phys) + ii;
373                 pdp = (uint64_t *)PHYS_TO_DMAP(*pml4 & PG_FRAME);
374                 i = (va >> PDPSHIFT) & ((1ul << NPDPEPGSHIFT) - 1);
375
376                 /* We always write a page, even if it is zero */
377                 if ((pdp[i] & PG_V) == 0) {
378                         error = blk_write(di, (char *)&fakepd, 0, PAGE_SIZE);
379                         if (error)
380                                 goto fail;
381                         /* flush, in case we reuse fakepd in the same block */
382                         error = blk_flush(di);
383                         if (error)
384                                 goto fail;
385                         continue;
386                 }
387
388                 /* 1GB page is represented as 512 2MB pages in a dump */
389                 if ((pdp[i] & PG_PS) != 0) {
390                         /* PDPE and PDP have identical layout in this case */
391                         fakepd[0] = pdp[i];
392                         for (j = 1; j < NPDEPG; j++)
393                                 fakepd[j] = fakepd[j - 1] + NBPDR;
394                         error = blk_write(di, (char *)&fakepd, 0, PAGE_SIZE);
395                         if (error)
396                                 goto fail;
397                         /* flush, in case we reuse fakepd in the same block */
398                         error = blk_flush(di);
399                         if (error)
400                                 goto fail;
401                         bzero(fakepd, sizeof(fakepd));
402                         continue;
403                 }
404
405                 pd = (uint64_t *)PHYS_TO_DMAP(pdp[i] & PG_FRAME);
406                 error = blk_write(di, (char *)pd, 0, PAGE_SIZE);
407                 if (error)
408                         goto fail;
409                 error = blk_flush(di);
410                 if (error)
411                         goto fail;
412         }
413
414         /* Dump memory chunks */
415         /* XXX cluster it up and use blk_dump() */
416         for (i = 0; i < vm_page_dump_size / sizeof(*vm_page_dump); i++) {
417                 bits = vm_page_dump[i];
418                 while (bits) {
419                         bit = bsfq(bits);
420                         pa = (((uint64_t)i * sizeof(*vm_page_dump) * NBBY) + bit) * PAGE_SIZE;
421                         error = blk_write(di, 0, pa, PAGE_SIZE);
422                         if (error)
423                                 goto fail;
424                         bits &= ~(1ul << bit);
425                 }
426         }
427
428         error = blk_flush(di);
429         if (error)
430                 goto fail;
431
432         /* Dump trailer */
433         error = dump_write(di, &kdh, 0, dumplo, sizeof(kdh));
434         if (error)
435                 goto fail;
436         dumplo += sizeof(kdh);
437
438         /* Signal completion, signoff and exit stage left. */
439         dump_write(di, NULL, 0, 0, 0);
440         printf("\nDump complete\n");
441         return;
442
443  fail:
444         if (error < 0)
445                 error = -error;
446
447         printf("\n");
448         if (error == ENOSPC) {
449                 printf("Dump map grown while dumping. ");
450                 if (retry_count < 5) {
451                         printf("Retrying...\n");
452                         goto retry;
453                 }
454                 printf("Dump failed.\n");
455         }
456         else if (error == ECANCELED)
457                 printf("Dump aborted\n");
458         else if (error == E2BIG)
459                 printf("Dump failed. Partition too small.\n");
460         else
461                 printf("** DUMP FAILED (ERROR %d) **\n", error);
462 }
463
464 void
465 dump_add_page(vm_paddr_t pa)
466 {
467         int idx, bit;
468
469         pa >>= PAGE_SHIFT;
470         idx = pa >> 6;          /* 2^6 = 64 */
471         bit = pa & 63;
472         atomic_set_long(&vm_page_dump[idx], 1ul << bit);
473 }
474
475 void
476 dump_drop_page(vm_paddr_t pa)
477 {
478         int idx, bit;
479
480         pa >>= PAGE_SHIFT;
481         idx = pa >> 6;          /* 2^6 = 64 */
482         bit = pa & 63;
483         atomic_clear_long(&vm_page_dump[idx], 1ul << bit);
484 }