]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/i386/i386/minidump_machdep.c
add -n option to suppress clearing the build tree and add -DNO_CLEAN
[FreeBSD/FreeBSD.git] / sys / i386 / i386 / 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 <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/conf.h>
33 #include <sys/cons.h>
34 #include <sys/kernel.h>
35 #include <sys/kerneldump.h>
36 #include <sys/msgbuf.h>
37 #include <sys/vimage.h>
38 #include <vm/vm.h>
39 #include <vm/pmap.h>
40 #include <machine/atomic.h>
41 #include <machine/elf.h>
42 #include <machine/md_var.h>
43 #include <machine/vmparam.h>
44 #include <machine/minidump.h>
45
46 CTASSERT(sizeof(struct kerneldumpheader) == 512);
47
48 /*
49  * Don't touch the first SIZEOF_METADATA bytes on the dump device. This
50  * is to protect us from metadata and to protect metadata from us.
51  */
52 #define SIZEOF_METADATA         (64*1024)
53
54 #define MD_ALIGN(x)     (((off_t)(x) + PAGE_MASK) & ~PAGE_MASK)
55 #define DEV_ALIGN(x)    (((off_t)(x) + (DEV_BSIZE-1)) & ~(DEV_BSIZE-1))
56
57 uint32_t *vm_page_dump;
58 int vm_page_dump_size;
59
60 static struct kerneldumpheader kdh;
61 static off_t dumplo;
62
63 /* Handle chunked writes. */
64 static size_t fragsz;
65 static void *dump_va;
66 static uint64_t counter, progress;
67
68 CTASSERT(sizeof(*vm_page_dump) == 4);
69
70 static int
71 is_dumpable(vm_paddr_t pa)
72 {
73         int i;
74
75         for (i = 0; dump_avail[i] != 0 || dump_avail[i + 1] != 0; i += 2) {
76                 if (pa >= dump_avail[i] && pa < dump_avail[i + 1])
77                         return (1);
78         }
79         return (0);
80 }
81
82 #define PG2MB(pgs) (((pgs) + (1 << 8) - 1) >> 8)
83
84 static int
85 blk_flush(struct dumperinfo *di)
86 {
87         int error;
88
89         if (fragsz == 0)
90                 return (0);
91
92         error = dump_write(di, dump_va, 0, dumplo, fragsz);
93         dumplo += fragsz;
94         fragsz = 0;
95         return (error);
96 }
97
98 static int
99 blk_write(struct dumperinfo *di, char *ptr, vm_paddr_t pa, size_t sz)
100 {
101         size_t len;
102         int error, i, c;
103         u_int maxdumpsz;
104
105         maxdumpsz = di->maxiosize;
106         if (maxdumpsz == 0)     /* seatbelt */
107                 maxdumpsz = PAGE_SIZE;
108         error = 0;
109         if ((sz % PAGE_SIZE) != 0) {
110                 printf("size not page aligned\n");
111                 return (EINVAL);
112         }
113         if (ptr != NULL && pa != 0) {
114                 printf("cant have both va and pa!\n");
115                 return (EINVAL);
116         }
117         if (pa != 0 && (((uintptr_t)ptr) % PAGE_SIZE) != 0) {
118                 printf("address not page aligned\n");
119                 return (EINVAL);
120         }
121         if (ptr != NULL) {
122                 /* If we're doing a virtual dump, flush any pre-existing pa pages */
123                 error = blk_flush(di);
124                 if (error)
125                         return (error);
126         }
127         while (sz) {
128                 len = maxdumpsz - fragsz;
129                 if (len > sz)
130                         len = sz;
131                 counter += len;
132                 progress -= len;
133                 if (counter >> 24) {
134                         printf(" %lld", PG2MB(progress >> PAGE_SHIFT));
135                         counter &= (1<<24) - 1;
136                 }
137                 if (ptr) {
138                         error = dump_write(di, ptr, 0, dumplo, len);
139                         if (error)
140                                 return (error);
141                         dumplo += len;
142                         ptr += len;
143                         sz -= len;
144                 } else {
145                         for (i = 0; i < len; i += PAGE_SIZE)
146                                 dump_va = pmap_kenter_temporary(pa + i, (i + fragsz) >> PAGE_SHIFT);
147                         fragsz += len;
148                         pa += len;
149                         sz -= len;
150                         if (fragsz == maxdumpsz) {
151                                 error = blk_flush(di);
152                                 if (error)
153                                         return (error);
154                         }
155                 }
156
157                 /* Check for user abort. */
158                 c = cncheckc();
159                 if (c == 0x03)
160                         return (ECANCELED);
161                 if (c != -1)
162                         printf(" (CTRL-C to abort) ");
163         }
164
165         return (0);
166 }
167
168 /* A fake page table page, to avoid having to handle both 4K and 2M pages */
169 static pt_entry_t fakept[NPTEPG];
170
171 void
172 minidumpsys(struct dumperinfo *di)
173 {
174         uint64_t dumpsize;
175         uint32_t ptesize;
176         vm_offset_t va;
177         int error;
178         uint32_t bits;
179         uint64_t pa;
180         pd_entry_t *pd;
181         pt_entry_t *pt;
182         int i, j, k, bit;
183         struct minidumphdr mdhdr;
184
185         counter = 0;
186         /* Walk page table pages, set bits in vm_page_dump */
187         ptesize = 0;
188         for (va = KERNBASE; va < kernel_vm_end; va += NBPDR) {
189                 /*
190                  * We always write a page, even if it is zero. Each
191                  * page written corresponds to 2MB of space
192                  */
193                 ptesize += PAGE_SIZE;
194                 pd = (pd_entry_t *)((uintptr_t)IdlePTD + KERNBASE);     /* always mapped! */
195                 j = va >> PDRSHIFT;
196                 if ((pd[j] & (PG_PS | PG_V)) == (PG_PS | PG_V))  {
197                         /* This is an entire 2M page. */
198                         pa = pd[j] & PG_PS_FRAME;
199                         for (k = 0; k < NPTEPG; k++) {
200                                 if (is_dumpable(pa))
201                                         dump_add_page(pa);
202                                 pa += PAGE_SIZE;
203                         }
204                         continue;
205                 }
206                 if ((pd[j] & PG_V) == PG_V) {
207                         /* set bit for each valid page in this 2MB block */
208                         pt = pmap_kenter_temporary(pd[j] & PG_FRAME, 0);
209                         for (k = 0; k < NPTEPG; k++) {
210                                 if ((pt[k] & PG_V) == PG_V) {
211                                         pa = pt[k] & PG_FRAME;
212                                         if (is_dumpable(pa))
213                                                 dump_add_page(pa);
214                                 }
215                         }
216                 } else {
217                         /* nothing, we're going to dump a null page */
218                 }
219         }
220
221         /* Calculate dump size. */
222         dumpsize = ptesize;
223         dumpsize += round_page(msgbufp->msg_size);
224         dumpsize += round_page(vm_page_dump_size);
225         for (i = 0; i < vm_page_dump_size / sizeof(*vm_page_dump); i++) {
226                 bits = vm_page_dump[i];
227                 while (bits) {
228                         bit = bsfl(bits);
229                         pa = (((uint64_t)i * sizeof(*vm_page_dump) * NBBY) + bit) * PAGE_SIZE;
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                         bits &= ~(1ul << bit);
237                 }
238         }
239         dumpsize += PAGE_SIZE;
240
241         /* Determine dump offset on device. */
242         if (di->mediasize < SIZEOF_METADATA + dumpsize + sizeof(kdh) * 2) {
243                 error = ENOSPC;
244                 goto fail;
245         }
246         dumplo = di->mediaoffset + di->mediasize - dumpsize;
247         dumplo -= sizeof(kdh) * 2;
248         progress = dumpsize;
249
250         /* Initialize mdhdr */
251         bzero(&mdhdr, sizeof(mdhdr));
252         strcpy(mdhdr.magic, MINIDUMP_MAGIC);
253         mdhdr.version = MINIDUMP_VERSION;
254         mdhdr.msgbufsize = msgbufp->msg_size;
255         mdhdr.bitmapsize = vm_page_dump_size;
256         mdhdr.ptesize = ptesize;
257         mdhdr.kernbase = KERNBASE;
258 #ifdef PAE
259         mdhdr.paemode = 1;
260 #endif
261
262         mkdumpheader(&kdh, KERNELDUMPMAGIC, KERNELDUMP_I386_VERSION, dumpsize, di->blocksize);
263
264         printf("Physical memory: %ju MB\n", ptoa((uintmax_t)physmem) / 1048576);
265         printf("Dumping %llu MB:", (long long)dumpsize >> 20);
266
267         /* Dump leader */
268         error = dump_write(di, &kdh, 0, dumplo, sizeof(kdh));
269         if (error)
270                 goto fail;
271         dumplo += sizeof(kdh);
272
273         /* Dump my header */
274         bzero(&fakept, sizeof(fakept));
275         bcopy(&mdhdr, &fakept, sizeof(mdhdr));
276         error = blk_write(di, (char *)&fakept, 0, PAGE_SIZE);
277         if (error)
278                 goto fail;
279
280         /* Dump msgbuf up front */
281         error = blk_write(di, (char *)msgbufp->msg_ptr, 0, round_page(msgbufp->msg_size));
282         if (error)
283                 goto fail;
284
285         /* Dump bitmap */
286         error = blk_write(di, (char *)vm_page_dump, 0, round_page(vm_page_dump_size));
287         if (error)
288                 goto fail;
289
290         /* Dump kernel page table pages */
291         for (va = KERNBASE; va < kernel_vm_end; va += NBPDR) {
292                 /* We always write a page, even if it is zero */
293                 pd = (pd_entry_t *)((uintptr_t)IdlePTD + KERNBASE);     /* always mapped! */
294                 j = va >> PDRSHIFT;
295                 if ((pd[j] & (PG_PS | PG_V)) == (PG_PS | PG_V))  {
296                         /* This is a single 2M block. Generate a fake PTP */
297                         pa = pd[j] & PG_PS_FRAME;
298                         for (k = 0; k < NPTEPG; k++) {
299                                 fakept[k] = (pa + (k * PAGE_SIZE)) | PG_V | PG_RW | PG_A | PG_M;
300                         }
301                         error = blk_write(di, (char *)&fakept, 0, PAGE_SIZE);
302                         if (error)
303                                 goto fail;
304                         /* flush, in case we reuse fakept in the same block */
305                         error = blk_flush(di);
306                         if (error)
307                                 goto fail;
308                         continue;
309                 }
310                 if ((pd[j] & PG_V) == PG_V) {
311                         pa = pd[j] & PG_FRAME;
312                         error = blk_write(di, 0, pa, PAGE_SIZE);
313                         if (error)
314                                 goto fail;
315                 } else {
316                         bzero(fakept, sizeof(fakept));
317                         error = blk_write(di, (char *)&fakept, 0, PAGE_SIZE);
318                         if (error)
319                                 goto fail;
320                         /* flush, in case we reuse fakept in the same block */
321                         error = blk_flush(di);
322                         if (error)
323                                 goto fail;
324                 }
325         }
326
327         /* Dump memory chunks */
328         /* XXX cluster it up and use blk_dump() */
329         for (i = 0; i < vm_page_dump_size / sizeof(*vm_page_dump); i++) {
330                 bits = vm_page_dump[i];
331                 while (bits) {
332                         bit = bsfl(bits);
333                         pa = (((uint64_t)i * sizeof(*vm_page_dump) * NBBY) + bit) * PAGE_SIZE;
334                         error = blk_write(di, 0, pa, PAGE_SIZE);
335                         if (error)
336                                 goto fail;
337                         bits &= ~(1ul << bit);
338                 }
339         }
340
341         error = blk_flush(di);
342         if (error)
343                 goto fail;
344
345         /* Dump trailer */
346         error = dump_write(di, &kdh, 0, dumplo, sizeof(kdh));
347         if (error)
348                 goto fail;
349         dumplo += sizeof(kdh);
350
351         /* Signal completion, signoff and exit stage left. */
352         dump_write(di, NULL, 0, 0, 0);
353         printf("\nDump complete\n");
354         return;
355
356  fail:
357         if (error < 0)
358                 error = -error;
359
360         if (error == ECANCELED)
361                 printf("\nDump aborted\n");
362         else if (error == ENOSPC)
363                 printf("\nDump failed. Partition too small.\n");
364         else
365                 printf("\n** DUMP FAILED (ERROR %d) **\n", error);
366 }
367
368 void
369 dump_add_page(vm_paddr_t pa)
370 {
371         int idx, bit;
372
373         pa >>= PAGE_SHIFT;
374         idx = pa >> 5;          /* 2^5 = 32 */
375         bit = pa & 31;
376         atomic_set_int(&vm_page_dump[idx], 1ul << bit);
377 }
378
379 void
380 dump_drop_page(vm_paddr_t pa)
381 {
382         int idx, bit;
383
384         pa >>= PAGE_SHIFT;
385         idx = pa >> 5;          /* 2^5 = 32 */
386         bit = pa & 31;
387         atomic_clear_int(&vm_page_dump[idx], 1ul << bit);
388 }
389