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