]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/i386/i386/minidump_machdep_base.c
Enable ROUTE_MPATH support in GENERIC kernels.
[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/vm_dumpset.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 #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(sizeof(dump_avail));
228         dumpsize += round_page(BITSET_SIZE(vm_page_dump_pages));
229         VM_PAGE_DUMP_FOREACH(pa) {
230                 /* Clear out undumpable pages now if needed */
231                 if (is_dumpable(pa)) {
232                         dumpsize += PAGE_SIZE;
233                 } else {
234                         dump_drop_page(pa);
235                 }
236         }
237         dumpsize += PAGE_SIZE;
238
239         progress = dumpsize;
240
241         /* Initialize mdhdr */
242         bzero(&mdhdr, sizeof(mdhdr));
243         strcpy(mdhdr.magic, MINIDUMP_MAGIC);
244         mdhdr.version = MINIDUMP_VERSION;
245         mdhdr.msgbufsize = msgbufp->msg_size;
246         mdhdr.bitmapsize = round_page(BITSET_SIZE(vm_page_dump_pages));
247         mdhdr.ptesize = ptesize;
248         mdhdr.kernbase = KERNBASE;
249         mdhdr.paemode = pae_mode;
250         mdhdr.dumpavailsize = round_page(sizeof(dump_avail));
251
252         dump_init_header(di, &kdh, KERNELDUMPMAGIC, KERNELDUMP_I386_VERSION,
253             dumpsize);
254
255         error = dump_start(di, &kdh);
256         if (error != 0)
257                 goto fail;
258
259         printf("Physical memory: %ju MB\n", ptoa((uintmax_t)physmem) / 1048576);
260         printf("Dumping %llu MB:", (long long)dumpsize >> 20);
261
262         /* Dump my header */
263         bzero(&fakept, sizeof(fakept));
264         bcopy(&mdhdr, &fakept, sizeof(mdhdr));
265         error = blk_write(di, (char *)&fakept, 0, PAGE_SIZE);
266         if (error)
267                 goto fail;
268
269         /* Dump msgbuf up front */
270         error = blk_write(di, (char *)msgbufp->msg_ptr, 0, round_page(msgbufp->msg_size));
271         if (error)
272                 goto fail;
273
274         /* Dump dump_avail */
275         _Static_assert(sizeof(dump_avail) <= sizeof(fakept),
276             "Large dump_avail not handled");
277         bzero(fakept, sizeof(fakept));
278         memcpy(fakept, dump_avail, sizeof(dump_avail));
279         error = blk_write(di, (char *)&fakept, 0, PAGE_SIZE);
280         if (error)
281                 goto fail;
282
283         /* Dump bitmap */
284         error = blk_write(di, (char *)vm_page_dump, 0,
285             round_page(BITSET_SIZE(vm_page_dump_pages)));
286         if (error)
287                 goto fail;
288
289         /* Dump kernel page table pages */
290         for (va = KERNBASE; va < kernel_vm_end; va += NBPDR) {
291                 /* We always write a page, even if it is zero */
292                 pd = IdlePTD;   /* always mapped! */
293                 j = va >> PDRSHIFT;
294                 if ((pd[j] & (PG_PS | PG_V)) == (PG_PS | PG_V))  {
295                         /* This is a single 2M block. Generate a fake PTP */
296                         pa = pd[j] & PG_PS_FRAME;
297                         for (k = 0; k < NPTEPG; k++) {
298                                 fakept[k] = (pa + (k * PAGE_SIZE)) | PG_V | PG_RW | PG_A | PG_M;
299                         }
300                         error = blk_write(di, (char *)&fakept, 0, PAGE_SIZE);
301                         if (error)
302                                 goto fail;
303                         /* flush, in case we reuse fakept in the same block */
304                         error = blk_flush(di);
305                         if (error)
306                                 goto fail;
307                         continue;
308                 }
309                 if ((pd[j] & PG_V) == PG_V) {
310                         pa = pd[j] & PG_FRAME;
311                         error = blk_write(di, 0, pa, PAGE_SIZE);
312                         if (error)
313                                 goto fail;
314                 } else {
315                         bzero(fakept, sizeof(fakept));
316                         error = blk_write(di, (char *)&fakept, 0, PAGE_SIZE);
317                         if (error)
318                                 goto fail;
319                         /* flush, in case we reuse fakept in the same block */
320                         error = blk_flush(di);
321                         if (error)
322                                 goto fail;
323                 }
324         }
325
326         /* Dump memory chunks */
327         VM_PAGE_DUMP_FOREACH(pa) {
328                 error = blk_write(di, 0, pa, PAGE_SIZE);
329                 if (error)
330                         goto fail;
331         }
332
333         error = blk_flush(di);
334         if (error)
335                 goto fail;
336
337         error = dump_finish(di, &kdh);
338         if (error != 0)
339                 goto fail;
340
341         printf("\nDump complete\n");
342         return (0);
343
344  fail:
345         if (error < 0)
346                 error = -error;
347
348         if (error == ECANCELED)
349                 printf("\nDump aborted\n");
350         else if (error == E2BIG || error == ENOSPC) {
351                 printf("\nDump failed. Partition too small (about %lluMB were "
352                     "needed this time).\n", (long long)dumpsize >> 20);
353         } else
354                 printf("\n** DUMP FAILED (ERROR %d) **\n", error);
355         return (error);
356 }