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