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