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