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