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