]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/i386/i386/minidump_machdep_base.c
Move vm_page_dump bitset array definition to MI code
[FreeBSD/FreeBSD.git] / sys / i386 / i386 / minidump_machdep_base.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_watchdog.h"
33
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/conf.h>
37 #include <sys/cons.h>
38 #include <sys/kernel.h>
39 #include <sys/kerneldump.h>
40 #include <sys/msgbuf.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/vmparam.h>
51 #include <machine/minidump.h>
52
53 CTASSERT(sizeof(struct kerneldumpheader) == 512);
54
55 #define MD_ALIGN(x)     (((off_t)(x) + PAGE_MASK) & ~PAGE_MASK)
56 #define DEV_ALIGN(x)    roundup2((off_t)(x), DEV_BSIZE)
57
58 static struct kerneldumpheader kdh;
59
60 /* Handle chunked writes. */
61 static size_t fragsz;
62 static void *dump_va;
63 static uint64_t counter, progress;
64
65 static int
66 is_dumpable(vm_paddr_t pa)
67 {
68         int i;
69
70         for (i = 0; dump_avail[i] != 0 || dump_avail[i + 1] != 0; i += 2) {
71                 if (pa >= dump_avail[i] && pa < dump_avail[i + 1])
72                         return (1);
73         }
74         return (0);
75 }
76
77 #define PG2MB(pgs) (((pgs) + (1 << 8) - 1) >> 8)
78
79 static int
80 blk_flush(struct dumperinfo *di)
81 {
82         int error;
83
84         if (fragsz == 0)
85                 return (0);
86
87         error = dump_append(di, dump_va, 0, fragsz);
88         fragsz = 0;
89         return (error);
90 }
91
92 static int
93 blk_write(struct dumperinfo *di, char *ptr, vm_paddr_t pa, size_t sz)
94 {
95         size_t len;
96         int error, i, c;
97         u_int maxdumpsz;
98
99         maxdumpsz = min(di->maxiosize, MAXDUMPPGS * PAGE_SIZE);
100         if (maxdumpsz == 0)     /* seatbelt */
101                 maxdumpsz = PAGE_SIZE;
102         error = 0;
103         if ((sz % PAGE_SIZE) != 0) {
104                 printf("size not page aligned\n");
105                 return (EINVAL);
106         }
107         if (ptr != NULL && pa != 0) {
108                 printf("cant have both va and pa!\n");
109                 return (EINVAL);
110         }
111         if (pa != 0 && (((uintptr_t)ptr) % PAGE_SIZE) != 0) {
112                 printf("address not page aligned\n");
113                 return (EINVAL);
114         }
115         if (ptr != NULL) {
116                 /* If we're doing a virtual dump, flush any pre-existing pa pages */
117                 error = blk_flush(di);
118                 if (error)
119                         return (error);
120         }
121         while (sz) {
122                 len = maxdumpsz - fragsz;
123                 if (len > sz)
124                         len = sz;
125                 counter += len;
126                 progress -= len;
127                 if (counter >> 24) {
128                         printf(" %lld", PG2MB(progress >> PAGE_SHIFT));
129                         counter &= (1<<24) - 1;
130                 }
131
132                 wdog_kern_pat(WD_LASTVAL);
133
134                 if (ptr) {
135                         error = dump_append(di, ptr, 0, len);
136                         if (error)
137                                 return (error);
138                         ptr += len;
139                         sz -= len;
140                 } else {
141                         for (i = 0; i < len; i += PAGE_SIZE)
142                                 dump_va = pmap_kenter_temporary(pa + i, (i + fragsz) >> PAGE_SHIFT);
143                         fragsz += len;
144                         pa += len;
145                         sz -= len;
146                         if (fragsz == maxdumpsz) {
147                                 error = blk_flush(di);
148                                 if (error)
149                                         return (error);
150                         }
151                 }
152
153                 /* Check for user abort. */
154                 c = cncheckc();
155                 if (c == 0x03)
156                         return (ECANCELED);
157                 if (c != -1)
158                         printf(" (CTRL-C to abort) ");
159         }
160
161         return (0);
162 }
163
164 /* A fake page table page, to avoid having to handle both 4K and 2M pages */
165 static pt_entry_t fakept[NPTEPG];
166
167 #ifdef PMAP_PAE_COMP
168 #define minidumpsys     minidumpsys_pae
169 #define IdlePTD         IdlePTD_pae
170 #else
171 #define minidumpsys     minidumpsys_nopae
172 #define IdlePTD         IdlePTD_nopae
173 #endif
174
175 int
176 minidumpsys(struct dumperinfo *di)
177 {
178         uint64_t dumpsize;
179         uint32_t ptesize;
180         vm_offset_t va;
181         int error;
182         uint64_t pa;
183         pd_entry_t *pd;
184         pt_entry_t *pt;
185         int j, k;
186         struct minidumphdr mdhdr;
187
188         counter = 0;
189         /* Walk page table pages, set bits in vm_page_dump */
190         ptesize = 0;
191         for (va = KERNBASE; va < kernel_vm_end; va += NBPDR) {
192                 /*
193                  * We always write a page, even if it is zero. Each
194                  * page written corresponds to 2MB of space
195                  */
196                 ptesize += PAGE_SIZE;
197                 pd = IdlePTD;   /* always mapped! */
198                 j = va >> PDRSHIFT;
199                 if ((pd[j] & (PG_PS | PG_V)) == (PG_PS | PG_V))  {
200                         /* This is an entire 2M page. */
201                         pa = pd[j] & PG_PS_FRAME;
202                         for (k = 0; k < NPTEPG; k++) {
203                                 if (is_dumpable(pa))
204                                         dump_add_page(pa);
205                                 pa += PAGE_SIZE;
206                         }
207                         continue;
208                 }
209                 if ((pd[j] & PG_V) == PG_V) {
210                         /* set bit for each valid page in this 2MB block */
211                         pt = pmap_kenter_temporary(pd[j] & PG_FRAME, 0);
212                         for (k = 0; k < NPTEPG; k++) {
213                                 if ((pt[k] & PG_V) == PG_V) {
214                                         pa = pt[k] & PG_FRAME;
215                                         if (is_dumpable(pa))
216                                                 dump_add_page(pa);
217                                 }
218                         }
219                 } else {
220                         /* nothing, we're going to dump a null page */
221                 }
222         }
223
224         /* Calculate dump size. */
225         dumpsize = ptesize;
226         dumpsize += round_page(msgbufp->msg_size);
227         dumpsize += round_page(BITSET_SIZE(vm_page_dump_pages));
228         VM_PAGE_DUMP_FOREACH(pa) {
229                 /* Clear out undumpable pages now if needed */
230                 if (is_dumpable(pa)) {
231                         dumpsize += PAGE_SIZE;
232                 } else {
233                         dump_drop_page(pa);
234                 }
235         }
236         dumpsize += PAGE_SIZE;
237
238         progress = dumpsize;
239
240         /* Initialize mdhdr */
241         bzero(&mdhdr, sizeof(mdhdr));
242         strcpy(mdhdr.magic, MINIDUMP_MAGIC);
243         mdhdr.version = MINIDUMP_VERSION;
244         mdhdr.msgbufsize = msgbufp->msg_size;
245         mdhdr.bitmapsize = round_page(BITSET_SIZE(vm_page_dump_pages));
246         mdhdr.ptesize = ptesize;
247         mdhdr.kernbase = KERNBASE;
248         mdhdr.paemode = pae_mode;
249
250         dump_init_header(di, &kdh, KERNELDUMPMAGIC, KERNELDUMP_I386_VERSION,
251             dumpsize);
252
253         error = dump_start(di, &kdh);
254         if (error != 0)
255                 goto fail;
256
257         printf("Physical memory: %ju MB\n", ptoa((uintmax_t)physmem) / 1048576);
258         printf("Dumping %llu MB:", (long long)dumpsize >> 20);
259
260         /* Dump my header */
261         bzero(&fakept, sizeof(fakept));
262         bcopy(&mdhdr, &fakept, sizeof(mdhdr));
263         error = blk_write(di, (char *)&fakept, 0, PAGE_SIZE);
264         if (error)
265                 goto fail;
266
267         /* Dump msgbuf up front */
268         error = blk_write(di, (char *)msgbufp->msg_ptr, 0, round_page(msgbufp->msg_size));
269         if (error)
270                 goto fail;
271
272         /* Dump bitmap */
273         error = blk_write(di, (char *)vm_page_dump, 0,
274             round_page(BITSET_SIZE(vm_page_dump_pages)));
275         if (error)
276                 goto fail;
277
278         /* Dump kernel page table pages */
279         for (va = KERNBASE; va < kernel_vm_end; va += NBPDR) {
280                 /* We always write a page, even if it is zero */
281                 pd = IdlePTD;   /* always mapped! */
282                 j = va >> PDRSHIFT;
283                 if ((pd[j] & (PG_PS | PG_V)) == (PG_PS | PG_V))  {
284                         /* This is a single 2M block. Generate a fake PTP */
285                         pa = pd[j] & PG_PS_FRAME;
286                         for (k = 0; k < NPTEPG; k++) {
287                                 fakept[k] = (pa + (k * PAGE_SIZE)) | PG_V | PG_RW | PG_A | PG_M;
288                         }
289                         error = blk_write(di, (char *)&fakept, 0, PAGE_SIZE);
290                         if (error)
291                                 goto fail;
292                         /* flush, in case we reuse fakept in the same block */
293                         error = blk_flush(di);
294                         if (error)
295                                 goto fail;
296                         continue;
297                 }
298                 if ((pd[j] & PG_V) == PG_V) {
299                         pa = pd[j] & PG_FRAME;
300                         error = blk_write(di, 0, pa, PAGE_SIZE);
301                         if (error)
302                                 goto fail;
303                 } else {
304                         bzero(fakept, sizeof(fakept));
305                         error = blk_write(di, (char *)&fakept, 0, PAGE_SIZE);
306                         if (error)
307                                 goto fail;
308                         /* flush, in case we reuse fakept in the same block */
309                         error = blk_flush(di);
310                         if (error)
311                                 goto fail;
312                 }
313         }
314
315         /* Dump memory chunks */
316         VM_PAGE_DUMP_FOREACH(pa) {
317                 error = blk_write(di, 0, pa, PAGE_SIZE);
318                 if (error)
319                         goto fail;
320         }
321
322         error = blk_flush(di);
323         if (error)
324                 goto fail;
325
326         error = dump_finish(di, &kdh);
327         if (error != 0)
328                 goto fail;
329
330         printf("\nDump complete\n");
331         return (0);
332
333  fail:
334         if (error < 0)
335                 error = -error;
336
337         if (error == ECANCELED)
338                 printf("\nDump aborted\n");
339         else if (error == E2BIG || error == ENOSPC) {
340                 printf("\nDump failed. Partition too small (about %lluMB were "
341                     "needed this time).\n", (long long)dumpsize >> 20);
342         } else
343                 printf("\n** DUMP FAILED (ERROR %d) **\n", error);
344         return (error);
345 }