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