i386: Move CPU ID and APIC ID maps from mp_machdep.c to lapic.c
[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
106         error = 0;
107         if ((sz % PAGE_SIZE) != 0) {
108                 kprintf("size not page aligned\n");
109                 return (EINVAL);
110         }
111         if (ptr != NULL && pa != 0) {
112                 kprintf("can't have both va and pa!\n");
113                 return (EINVAL);
114         }
115         if (pa != 0 && (((uintptr_t)ptr) % PAGE_SIZE) != 0) {
116                 kprintf("address not page aligned\n");
117                 return (EINVAL);
118         }
119         if (ptr != NULL) {
120                 /* If we're doing a virtual dump, flush any pre-existing pa pages */
121                 error = blk_flush(di);
122                 if (error)
123                         return (error);
124         }
125         while (sz) {
126                 len = (MAXDUMPPGS * PAGE_SIZE) - fragsz;
127                 if (len > sz)
128                         len = sz;
129                 counter += len;
130                 progress -= len;
131                 if (counter >> 24) {
132                         kprintf(" %lld", PG2MB(progress >> PAGE_SHIFT));
133                         counter &= (1<<24) - 1;
134                 }
135                 if (ptr) {
136                         error = dev_ddump(di->priv, ptr, 0, dumplo, len);
137                         if (error)
138                                 return (error);
139                         dumplo += len;
140                         ptr += len;
141                         sz -= len;
142                 } else {
143                         for (i = 0; i < len; i += PAGE_SIZE) {
144                                 dump_va = pmap_kenter_temporary(pa + i,
145                                                 (i + fragsz) >> PAGE_SHIFT);
146                         }
147                         smp_invltlb();
148                         fragsz += len;
149                         pa += len;
150                         sz -= len;
151                         if (fragsz == (MAXDUMPPGS * PAGE_SIZE)) {
152                                 error = blk_flush(di);
153                                 if (error)
154                                         return (error);
155                         }
156                 }
157
158                 /* Check for user abort. */
159                 c = cncheckc();
160                 if (c == 0x03)
161                         return (ECANCELED);
162                 if (c != -1)
163                         kprintf(" (CTRL-C to abort) ");
164         }
165
166         return (0);
167 }
168
169 /* A fake page table page, to avoid having to handle both 4K and 2M pages */
170 static pt_entry_t fakept[NPTEPG];
171
172 void
173 minidumpsys(struct dumperinfo *di)
174 {
175         uint64_t dumpsize;
176         uint32_t ptesize;
177         vm_offset_t va;
178         vm_offset_t kern_end;
179         int error;
180         uint32_t bits;
181         uint64_t pa;
182         pd_entry_t *pd;
183         pt_entry_t *pt;
184         int i, j, k, bit;
185         struct minidumphdr mdhdr;
186         struct mdglobaldata *md;
187
188         counter = 0;
189         ptesize = 0;
190
191         md = (struct mdglobaldata *)globaldata_find(0);
192
193         kern_end = kernel_vm_end;
194         if (kern_end < (vm_offset_t)&(md[ncpus]))
195                 kern_end = (vm_offset_t)&(md[ncpus]);
196 #if 0
197         kern_end = 0xFFFFF000;
198 #endif
199
200         /* Walk page table pages, set bits in vm_page_dump */
201         for (va = KERNBASE; va < kern_end; va += NBPDR) {
202                 /*
203                  * We always write a page, even if it is zero. Each
204                  * page written corresponds to 2MB of space
205                  */
206                 ptesize += PAGE_SIZE;
207                 pd = (pd_entry_t *)((uintptr_t)IdlePTD + KERNBASE);     /* always mapped! */
208                 j = va >> PDRSHIFT;
209                 if ((pd[j] & (PG_PS | PG_V)) == (PG_PS | PG_V))  {
210                         /* This is an entire 2M page. */
211                         pa = pd[j] & PG_FRAME & ~PDRMASK;
212                         for (k = 0; k < NPTEPG; k++) {
213                                 if (is_dumpable(pa))
214                                         dump_add_page(pa);
215                                 pa += PAGE_SIZE;
216                         }
217                         continue;
218                 }
219                 if ((pd[j] & PG_V) == PG_V) {
220                         /* set bit for each valid page in this 2MB block */
221                         pt = pmap_kenter_temporary(pd[j] & PG_FRAME, 0);
222                         smp_invltlb();
223                         for (k = 0; k < NPTEPG; k++) {
224                                 if ((pt[k] & PG_V) == PG_V) {
225                                         pa = pt[k] & PG_FRAME;
226                                         if (is_dumpable(pa))
227                                                 dump_add_page(pa);
228                                 }
229                         }
230                 } else {
231                         /* nothing, we're going to dump a null page */
232                 }
233         }
234
235         /* Calculate dump size. */
236         dumpsize = ptesize;
237         dumpsize += round_page(msgbufp->msg_size);
238         dumpsize += round_page(vm_page_dump_size);
239         for (i = 0; i < vm_page_dump_size / sizeof(*vm_page_dump); i++) {
240                 bits = vm_page_dump[i];
241                 while (bits) {
242                         bit = bsfl(bits);
243                         pa = (((uint64_t)i * sizeof(*vm_page_dump) * NBBY) + bit) * PAGE_SIZE;
244                         /* Clear out undumpable pages now if needed */
245                         if (is_dumpable(pa)) {
246                                 dumpsize += PAGE_SIZE;
247                         } else {
248                                 dump_drop_page(pa);
249                         }
250                         bits &= ~(1ul << bit);
251                 }
252         }
253         dumpsize += PAGE_SIZE;
254
255         /* Determine dump offset on device. */
256         if (di->mediasize < SIZEOF_METADATA + dumpsize + sizeof(kdh) * 2) {
257                 error = ENOSPC;
258                 goto fail;
259         }
260         dumplo = di->mediaoffset + di->mediasize - dumpsize;
261         dumplo -= sizeof(kdh) * 2;
262         progress = dumpsize;
263
264         /* Initialize mdhdr */
265         bzero(&mdhdr, sizeof(mdhdr));
266         strcpy(mdhdr.magic, MINIDUMP_MAGIC);
267         mdhdr.version = MINIDUMP_VERSION;
268         mdhdr.msgbufsize = msgbufp->msg_size;
269         mdhdr.bitmapsize = vm_page_dump_size;
270         mdhdr.ptesize = ptesize;
271         mdhdr.kernbase = KERNBASE;
272
273         mkdumpheader(&kdh, KERNELDUMPMAGIC, KERNELDUMP_I386_VERSION,
274             dumpsize, di->blocksize);
275
276         kprintf("Physical memory: %jd MB\n", (intmax_t)ptoa(physmem) / 1048576);
277         kprintf("Dumping %jd MB:", (intmax_t)dumpsize >> 20);
278
279         /* Dump leader */
280         error = dev_ddump(di->priv, &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 < kern_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_FRAME & ~PDRMASK;
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 = pd[j] & PG_FRAME;
324                         error = blk_write(di, 0, pa, PAGE_SIZE);
325                         if (error)
326                                 goto fail;
327                 } else {
328                         bzero(fakept, sizeof(fakept));
329                         error = blk_write(di, (char *)&fakept, 0, PAGE_SIZE);
330                         if (error)
331                                 goto fail;
332                         /* flush, in case we reuse fakept in the same block */
333                         error = blk_flush(di);
334                         if (error)
335                                 goto fail;
336                 }
337         }
338
339         /* Dump memory chunks */
340         /* XXX cluster it up and use blk_dump() */
341         for (i = 0; i < vm_page_dump_size / sizeof(*vm_page_dump); i++) {
342                 bits = vm_page_dump[i];
343                 while (bits) {
344                         bit = bsfl(bits);
345                         pa = (((uint64_t)i * sizeof(*vm_page_dump) * NBBY) + bit) * PAGE_SIZE;
346                         error = blk_write(di, 0, pa, PAGE_SIZE);
347                         if (error)
348                                 goto fail;
349                         bits &= ~(1ul << bit);
350                 }
351         }
352
353         error = blk_flush(di);
354         if (error)
355                 goto fail;
356
357         /* Dump trailer */
358         error = dev_ddump(di->priv, &kdh, 0, dumplo, sizeof(kdh));
359         if (error)
360                 goto fail;
361         dumplo += sizeof(kdh);
362
363         /* Signal completion, signoff and exit stage left. */
364         dev_ddump(di->priv, NULL, 0, 0, 0);
365         kprintf("\nDump complete\n");
366         return;
367
368  fail:
369         if (error < 0)
370                 error = -error;
371
372         if (error == ECANCELED)
373                 kprintf("\nDump aborted\n");
374         else if (error == ENOSPC)
375                 kprintf("\nDump failed. Partition too small.\n");
376         else
377                 kprintf("\n** DUMP FAILED (ERROR %d) **\n", error);
378 }
379
380 void
381 dump_add_page(vm_paddr_t pa)
382 {
383         int idx, bit;
384
385         pa >>= PAGE_SHIFT;
386         idx = pa >> 5;          /* 2^5 = 32 */
387         bit = pa & 31;
388         atomic_set_int(&vm_page_dump[idx], 1ul << bit);
389 }
390
391 void
392 dump_drop_page(vm_paddr_t pa)
393 {
394         int idx, bit;
395
396         pa >>= PAGE_SHIFT;
397         idx = pa >> 5;          /* 2^5 = 32 */
398         bit = pa & 31;
399         atomic_clear_int(&vm_page_dump[idx], 1ul << bit);
400 }