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