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