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