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